如何使用文件名从程序集中加载用户控件?

发布于 2024-08-15 04:34:31 字数 228 浏览 1 评论 0原文

我们有一组符合 dll 的用户控件。我们可以使用类名加载控件,例如 ASP.theusercontrol_ascx blah = new ASP.theusercontrol_ascx();

但如果我们有动态数据并根据文件名(或控件名称)加载控件, 我们如何像 Page.LoadControl("TheURL/theusercontrol.ascx") 一样加载它们?

多谢...

we have a set of usercontrols complied to a dll. we could just load the controls using their classnames by like ASP.theusercontrol_ascx blah = new ASP.theusercontrol_ascx();

but if we have dynamic data and load the controls based on the filename (or the control name),
how can we load them like Page.LoadControl("TheURL/theusercontrol.ascx")?

Thanks a lot...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-08-22 04:34:31

确实没有正确的方法来做到这一点。 .ascx 文件并不意味着可以通过这种方式进行部署。代码已编译为 .dll,但 .ascx 文件中的实际标记尚未编译。即使您遵循有关将 .ascx 文件转换为可部署控件,您将失去根据文件名调用 LoadControl 的能力,仅仅是因为不再有标记文件。

有许多黑客让您可以通过反射并将 .ascx 文件作为资源嵌入来完成。

除此之外,如果您确实需要使用 LoadControl(filename),则需要复制 .ascx 文件。

There isn't really a proper way of doing this. .ascx files are not meant to be deployable this way. The code has been compiled to the .dll, but the actual markup from the .ascx file has not. Even if you follow the official Microsoft documentation on converting .ascx files to deployable controls, you lose the ability to call LoadControl based on file name, simply because there is no markup file anymore.

There are a number of hacks floating around that let you do it via reflection and embedding the .ascx files as resources.

Aside from that, if you really need to use LoadControl(filename), you will need to copy over the .ascx files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文