LoadControl VS构建ASP.Net控制 [英] LoadControl vs Construct ASP.Net Control

查看:142
本文介绍了LoadControl VS构建ASP.Net控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,为什么我们只能添加使用LoadControl动态控制。
例如:

I have a question why we can only add dynamic control using LoadControl. For example:

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的子控件为空,但是当我使用下面的方法:

When in the page_load method of wucDate, the child control of wucDate is null but when i use the following method:

   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的子控件不为空。
是任何人都可以向我解释为什么ASP.NET的不创建在使用构造器我wucDate的任何子控制?谢谢

In the page_load method of wucDate, the child control of wucDate is not null. Is anyone could explain to me why asp .net don't create any child control of wucDate when i using contructor ??? Thank you

推荐答案

在动态加载的用户控件,重要的是要确保标准ASP.NET页面事件管线启动并正常进展。当您使用new运算符来创建用户控件的实例,该用户控件没有正确添加到ASP.NET的事件系统。如果事件(初始化,加载,preRender等),以不火,那么你的控制将永远不会正常工作。这就是为什么必须使用LoadControl,因为这将确保您的用户控件的实例正确创建并连接到ASP.NET。

When dynamically loading a user control, it is important to ensure that the standard ASP.NET page event pipeline is initiated and progresses normally. When you use the new operator to create an instance of a user control, that user control is not properly added to ASP.NET's event system. If the events (Init, Load, PreRender, etc.) to not fire, then your control will never function properly. That is why it is necessary to use LoadControl, as that will make sure that the instance of your user control is created properly and attached to ASP.NET.

这篇关于LoadControl VS构建ASP.Net控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆