如何将用户控件从 dll 动态加载到 asp:panel 中
我有超过 3 个网站。所以我想在这些应用程序中分享一些常见的用户控件。为此,我创建了一个新的 Web 应用程序,具有常见的用户控件(即登录)和一些其他控件。然后我生成 ddl,使用下面的代码将其加载到我的其他网站中:
但是我无法使用下面的代码将用户控件从 dll 加载到面板中,就像我们使用 Control c = Page.LoadContnrol("contrlname. ascx”)。
Assembly assembly = Assembly.LoadFrom(pluginPath);
Type controlType = assembly.GetType("ABCCommon.controls.LoginPanel");
object controlss = Activator.CreateInstance(controlType);
// this section doesn't load the user control from dll into panel
pnlLoginControl.Controls.Add((Control)c);
或
Assembly plugin = Assembly.LoadFrom(pluginPath);
Type[] types = plugin.GetTypes();
foreach (Type t in types)
{
if (typeof(UserControl).IsAssignableFrom(t))
{
UserControl control = (UserControl)Activator.CreateInstance(t);
controls.Add(control);
}
}
UserControl c = (UserControl)controls[0];
// 此部分不会将用户控件从 dll 加载到面板中
this.pnlLoginControl.Controls.Add(c);
请有人帮忙吗? 谢谢!
I have more than 3 websites. So I want to share some common user controls among these applications. For this purpose i have created a new web application having common user controls i.e login, and some other controls. Then i produce the ddl which i load into my other web sites using below code:
But i unable to load the user control from dll into panel using below code as we do by using Control c = Page.LoadContnrol("contrlname.ascx")
.
Assembly assembly = Assembly.LoadFrom(pluginPath);
Type controlType = assembly.GetType("ABCCommon.controls.LoginPanel");
object controlss = Activator.CreateInstance(controlType);
// this section doesn't load the user control from dll into panel
pnlLoginControl.Controls.Add((Control)c);
or
Assembly plugin = Assembly.LoadFrom(pluginPath);
Type[] types = plugin.GetTypes();
foreach (Type t in types)
{
if (typeof(UserControl).IsAssignableFrom(t))
{
UserControl control = (UserControl)Activator.CreateInstance(t);
controls.Add(control);
}
}
UserControl c = (UserControl)controls[0];
// this section doesn't load the user control from dll into panel
this.pnlLoginControl.Controls.Add(c);
Please can anyone help?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用 VirtualPathProvider 来执行此操作。问题是 Controls.Add(c) 期望找到 ascx 文件,但该文件位于另一个项目中。
本教程 解释要采取的步骤。
You have to use the VirtualPathProvider to do this. The problem is that Controls.Add(c) expects to find the ascx file but that's in another project.
This tutorial explains the step to take.
程序集中未指定用户控件。用户控件由 .ascx 文件定义,并由 asp.net 在运行时在临时程序集中进行编译。
要实例化用户控件,您应该使用方法
Page.LoadControl
。A user control is not specified in an assembly. The user control is defined by the .ascx file and gets compiled by asp.net on runtime in a temporary assembly.
To instantiate user controls you should use the method
Page.LoadControl
.如果您在项目中添加对 DLL 的引用,并使用接受类型(以及传递给构造函数的任何参数)的重载
LoadControl
方法:第二个参数用于将参数传递给参数化构造函数。
编辑
如果这些是自定义服务器控件,只需在项目中添加对 DLL 的引用,然后在使用该控件的任何页面上导入程序集:
使用该控件:
If you add a reference to the DLL in your project, and use the overloaded
LoadControl
method that accepts a type (and any parameters passed to the constructor):The second argument is for passing arguments into a parameterized constructor.
EDIT
If these are custom server controls, just add a refernce to the DLL in your project and import the assembly on whatever pages are using the control:
Use the control: