我有 ASPX 页面,在预初始化时我检查要加载的用户控件。
control = "~/templates/" + which + "/master.ascx";
然后在页面加载时加载该控件
Control userControl = Page.LoadControl(control);
Page.Controls.Add(userControl);
我如何将动态加载的用户控件从 aspx 传输到 ascx?
您可以创建一个由所有自定义控件实现的界面。这样,您就可以转换到该接口并使用它来传递数据。考虑这个例子:
public interface ICustomControl
{
string SomeProperty { get; set; }
}
...以及您的控制:
public class Control1 : Control, ICustomControl
{
public string SomeProperty
{
get { return someControl.Text; }
set { someControl.Text = value; }
}
// ...
}
现在,您可以执行以下操作:
Control userControl = Page.LoadControl(control);
Page.Controls.Add(userControl);
if (userControl is ICustomControl)
{
ICustomControl customControl = userControl as ICustomControl;
customControl.SomeProperty = "Hello, world!";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)