如何使用文件名从程序集中加载用户控件?
我们有一组符合 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实没有正确的方法来做到这一点。 .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.