总结Asp.net中Page加载PostData的具体过程 进而解决"获取动态创建的控件的PostData数据"问题...

Asp.net中Page加载PostData过程:

1、触发页面init事件。
2、判断是否IsPostBack,如果True,就走第3步,否则跳过第3步
3、解析PostData数据,并根据页面的设计内容(.aspx),加载PostData(一次加载)到各个控件。
4、 触发页面的Load事件(在这个事件中有一个机会让我们来准备Load动态创建控件的PostData)
5、再次判断是否IsPostBack,如果True,就走第6步,否则跳过第6步
6、解析PostData数据,判断是否还有剩余的PostData还没有被加载?如果是,进行第二次加载。否则退出
..........
说明:PostData的数据的加载规则: 根据PostData(其实是一个Collection对象)中的控件Name与当前Page的Control中的控件对象的Name是否匹配,如果匹配就把Value赋给对应控件对象的某个属性。

也许你们已经发现 剩余的PostData是不是就是动态创建的控件呈现到客户端,又从客户端Post过来的数据呢? 答案是完全正确的。 剩余的PostData确实是从动态创建的控件对应的Post数据。 流程的第6步表示: Asp.net在Page_Load事件后,还会再一次加载剩余的PostData,这就是在很多文章中所提及的二次加载

比如是在某个Button的Click事件中创建了CheckBox,以成员变量CheckBox1引用,控件Name为CheckBox1。然后呈现给客户端,客户端选中该CheckBox1,并又提交。由于是动态创建的控件原因,在服务器处理这次客户端的提交时,在Page_Load是无法获取该CheckBox1的是否被选中的状态的。应该这样处理:在Page_Load中再一次创建该CheckbBox1,以成员变量CheckBox1引用,控件Name为CheckBox1,该对象的Name必须和前一次所动态创建控件的Name必须相同),并返回,然后在其他控件事件中获取该checkbox1变量的check属性值就是客户端的用户操作的实际值。

所以,在Page的Load事件的处理方式是实现“获取动态创建控件的PostData”的关键点!为什么这么说呢?因为只有在这个Load事件中,我们才有机会把前一次创建的动态控件再创建一次,在让Asp.net的Page来做"第5步和第6步",达到把剩余的PostData加载到刚刚所创建的控件对象去( 这就是Postdata的二次加载)....


我不知道解释是否详细?如果有不明白的地方非常抱歉,大家可以自己去MSDN上查找....我就不替大家找了...但是我这里给出一个实例代码,用于解释“如何获取动态创建控件的PostData数据”,仔细研究一下,我想也差不多够了,至于代码中有不明白的地方,可随时提问!希望对大家有帮助! :-)

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  AspTestForFaq
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for WebForm6.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm6 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Button CreateCtrl;
InBlock.gif        
protected System.Web.UI.WebControls.Button Submit;
InBlock.gif        
protected System.Web.UI.WebControls.Panel Panel1;
InBlock.gif
InBlock.gif        
private CheckBox m_CheckBox = null;
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Put user code to initialize the page here
InBlock.gif
            if (IsPostBack && (null != ViewState["Created"]))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_CheckBox 
= new CheckBox();
InBlock.gif                Panel1.Controls.Add(m_CheckBox);
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

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

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void CreateCtrl_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (ViewState["Created"==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_CheckBox 
= new CheckBox();
InBlock.gif                m_CheckBox.Text 
= "Click me";
InBlock.gif                Panel1.Controls.Add(m_CheckBox);
InBlock.gif                ViewState[
"Created"= "1";
InBlock.gif                CreateCtrl.Visible 
= false;
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Submit_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (m_CheckBox != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (m_CheckBox.Checked)
InBlock.gif                    m_CheckBox.Text 
= "Thank you for your check";
InBlock.gif                
else
InBlock.gif                    m_CheckBox.Text 
= "please checked me!!!";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/caomao/archive/2005/06/16/175651.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值