ASP.NET动态加载用户控件的页面生成过程
MainPage文件:WebForm1.aspx
WebForm1.aspx.cs
ASCX文件:search.ascx
<%@ 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>
<!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 窗体设计器生成的代码
}
}
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 窗体设计器生成的代码
}
}
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">
<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 窗体设计器生成的代码
protected override void OnPreRender(EventArgs e)
{
TextBox1.Text = TextBox1.Text + "a1";//执行前TextBox1.Text会被用户提交的数据比如"aaaaa"所覆盖,所以用户提交的数据在这里才能获取
base.OnPreRender(e);
//TextBox1.Text = "a2";
}
}
}
{
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 窗体设计器生成的代码
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的数据