use*_*996 9 asp.net controls dynamic
我有一个问题,为什么我们只能使用LoadControl添加动态控件.例如:
public partial class wucReportParam : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
wucDate() ctrl = new wucDate();
pnl.Controls.Add(ctrl);
}
}
Run Code Online (Sandbox Code Playgroud)
在wucDate的page_load方法中,wucDate的子控件为null但是当我使用以下方法时:
public partial class wucReportParam : System.Web.UI.UserControl
{
public Report Report;
protected void Page_Load(object sender, EventArgs e)
{
ctrl = (wucDate)LoadControl(@"Reports\wucDate.ascx");
pnl.Controls.Add(ctrl);
}
}
Run Code Online (Sandbox Code Playgroud)
在wucDate的page_load方法中,wucDate的子控件不为null.是否有人可以向我解释为什么当我使用contructor时,asp .net不会创建任何wucDate的子控件?谢谢
动态加载用户控件时,务必确保启动标准ASP.NET页面事件管道并正常进行.使用new运算符创建用户控件的实例时,该用户控件未正确添加到ASP.NET的事件系统中.如果事件(Init,Load,PreRender等)没有触发,那么你的控件将无法正常运行.这就是为什么有必要使用LoadControl,因为这将确保正确创建用户控件的实例并附加到ASP.NET.
归档时间: |
|
查看次数: |
3733 次 |
最近记录: |