LoadControl与构造ASP.Net控件

前端之家收集整理的这篇文章主要介绍了LoadControl与构造ASP.Net控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个问题,为什么我们只能使用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);
    }
}

在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);
        }
    }

在wucDate的page_load方法中,wucDate的子控件不为null.
是否有人可以向我解释为什么当我使用contructor时,asp .net不会创建任何wucDate的子控件?谢谢

解决方法

动态加载用户控件时,务必确保启动标准ASP.NET页面事件管道并正常进行.当您使用new运算符创建用户控件的实例时,该用户控件未正确添加到ASP.NET的事件系统中.如果事件(Init,Load,PreRender等)没有触发,那么你的控件永远不会正常工作.这就是为什么有必要使用LoadControl,因为这将确保正确创建用户控件的实例并附加到ASP.NET.
原文链接:https://www.f2er.com/aspnet/248931.html

猜你在找的asp.Net相关文章