[英]How to access user control properties if i load it programatically?
我以编程方式加载了我的用户控件(.ascx),例如: LoadControl("~/Controls/mycontrol.ascx")
。 一切顺利,直到今天,当我将两个成员添加到我的控件中时:
public StuffType stuffType { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
switch (stuffType)
{
case CardType.A:
FillGvStuff();
break;
case CardType.B:
FillGvExStuff();
break;
default:
break;
}
}
如何访问StuffType?
我找到了一种解决方案 。
我想你会做这样的事情:
MyControl ctrl = (MyControl)LoadControl("~/Controls.mycontrol.ascx");
ctrl.stuffType = ...;
// put control somehwere
基本上,当您加载它时,将其分配给一个变量并将其转换为它的类型,然后应该可以访问其方法和属性
您可能还需要将page_load事件移到page_prerender中,以便在控件中的page_load事件发生之前明确设置属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.