ASP.NET动态加载用户控件的页面生成过程

  MainPage文件:WebForm1.aspx

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestMasterPage.WebForm1" enableViewState="False"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

    <HTML>

    <HEAD>

    <title>WebForm1</title>

    </HEAD>

    <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">

    <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder></form>

    </body>

    </HTML>

    WebForm1.aspx.cs

    using System;

    using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

    namespace TestMasterPage

    {

    /// <summary>

    /// WebForm1 的摘要说明。

    /// </summary>

    public class WebForm1 : System.Web.UI.Page

    {

    protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1; private void Page_Load(object sender, System.EventArgs e)

    {

    //在此处放置用户代码以初始化页面

    string controlName = "search.ascx";

    UserControl control = (UserControl)LoadControl("~/skins/default/controls/"+ controlName);

    control.ID = "ID_" + controlName; PlaceHolder1.Controls.Add(control); Response.Write("only trace..");

    }

    Web窗体设计器生成的代码Web窗体设计器生成的代码

    override protected void OnInit(EventArgs e)

    {

    //

    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

    //

    InitializeComponent();

    base.OnInit(e);

    }

    /// <summary>

    ///设计器支持所需的方法-不要使用代码编辑器修改

    /// 此方法的内容。

    /// </summary>

    private void InitializeComponent()

    {

    this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion

    }

    }

 

 ASCX文件:search.ascx

    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="search.ascx.cs" Inherits="TestMasterPage.Skins.controls.search" enableViewState="False"%>

    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

    <INPUT type="submit" value="Submit">

    search.ascx.cs

    namespace TestMasterPage.Skins.controls

    {

    using System;

    using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

    /// <summary>

    ///        search 的摘要说明。

    /// </summary>

    public class search : System.Web.UI.UserControl

    {

    protected System.Web.UI.WebControls.TextBox TextBox1;

    private void Page_Load(object sender, System.EventArgs e)

    {

    //在此处放置用户代码以初始化页面

    if (IsPostBack)

    {

    TextBox1.Text = TextBox1.Text + "aa";

    }

    }

    Web窗体设计器生成的代码Web窗体设计器生成的代码

    override protected void OnInit(EventArgs e)

    {

    //

    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

    //

    TextBox1.Text = TextBox1.Text + "a3";//执行前TextBox1.Text="",每次初始化时候TextBox1.Text都为空

    InitializeComponent();

    base.OnInit(e); TextBox1.Text = "a4";

    }

    /// <summary>

    ///设计器支持所需的方法-不要使用代码编辑器

    ///        修改此方法的内容。

    /// </summary>

    private void InitializeComponent()

    {

    this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion

    protected override void OnPreRender(EventArgs e)

    {

    TextBox1.Text = TextBox1.Text + "a1";//执行前TextBox1.Text会被用户提交的数据比如"aaaaa"所覆盖,所以用户提交的数据在这里才能获取

    base.OnPreRender(e); //TextBox1.Text = "a2";

    }

    }

    }

    执行顺序

    WebForm1.aspx.cs> Page_Load() //加载WebForm1.ascx页面WebForm1.aspx.cs> Page_Load()> PlaceHolder1.Controls.Add(control); //这个时候新加载的control的数据都是空的,包括postback的数据也是空的search.ascx.cs> OnInit() WebForm1.aspx.cs> Page_Load() //返回,继续search.ascx.cs> Page_Load()  //加载search.ascx页面search.ascx.cs> OnPreRender() //生成search。ascx的html,这个时候会加载postback的数据

 

 

 

http://dotnet.chinaitlab.com/ASPNET/795698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值