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

MainPage文件:WebForm1.aspx

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestMasterPage.WebForm1" enableViewState="False" %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< asp:PlaceHolder  id ="PlaceHolder1"  runat ="server" ></ asp:PlaceHolder ></ form >
None.gif    
</ body >
None.gif
</ HTML >

WebForm1.aspx.cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  TestMasterPage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif                
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;InBlock.gif    
InBlock.gif                
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            string controlName = "search.ascx";
InBlock.gif            UserControl control 
=  (UserControl)LoadControl("~/skins/default/controls/"+ controlName);
InBlock.gif            control.ID 
= "ID_" + controlName;
InBlock.gif            PlaceHolder1.Controls.Add(control);
InBlock.gif            Response.Write(
"only trace..");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ASCX文件:search.ascx
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Control Language="c#" AutoEventWireup="false" Codebehind="search.ascx.cs" Inherits="TestMasterPage.Skins.controls.search" enableViewState="False" %>
None.gif
< asp:TextBox  id ="TextBox1"  runat ="server" ></ asp:TextBox >
None.gif
< INPUT  type ="submit"  value ="Submit" >
None.gif

search.ascx.cs

None.gif namespace  TestMasterPage.Skins.controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Drawing;
InBlock.gif    
using System.Web;
InBlock.gif    
using System.Web.UI.WebControls;
InBlock.gif    
using System.Web.UI.HtmlControls;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///        search 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class search : System.Web.UI.UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
                protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif
InBlock.gif             
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if (IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TextBox1.Text 
= TextBox1.Text + "aa";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            TextBox1.Text = TextBox1.Text + "a3";//执行前TextBox1.Text="",每次初始化时候TextBox1.Text都为空
InBlock.gif

InBlock.gif            InitializeComponent();
InBlock.gif            
base.OnInit(e);
InBlock.gif            TextBox1.Text 
= "a4";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        设计器支持所需的方法 - 不要使用代码编辑器
InBlock.gif        
///        修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
protected override void OnPreRender(EventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            TextBox1.Text 
= TextBox1.Text + "a1";//执行前TextBox1.Text会被用户提交的数据比如"aaaaa"所覆盖,所以用户提交的数据在这里才能获取
InBlock.gif

InBlock.gif            
base.OnPreRender(e); 
InBlock.gif            
//TextBox1.Text = "a2";
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


执行顺序
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的数据

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值