Summarize the specific process of loading PostData on pages in Asp.net to solve the problem of "retrieving PostData data of dynamically created controls ".

Source: Internet
Author: User
In Asp.net, The PostData loading process is as follows:

1. Trigger the page init event.
2. Determine whether IsPostBack is enabled. If it is True, skip step 1. Otherwise, skip step 2.
3. parse the PostData data and load the PostData (one-time loading) to each control based on the design content of the page (. aspx.
4. Trigger the page Load event (this event has an opportunity for us to prepare the Load to dynamically create the control's PostData ).
5. Determine whether IsPostBack is enabled again. If it is True, skip step 1. Otherwise, skip step 2.
6. parse the PostData data and determine if there is any remaining PostData not loaded yet? If yes, perform the second loading. Otherwise exit
..........
Note: The data loading rules for PostData are as follows:Based on whether the Control Name in PostData (actually a Collection object) matches the Name of the Control object in the current Page Control, if it matches, Value is assigned to a property of the corresponding Control object.

You may have discoveredRemaining PostDataIs it the data that is dynamically created controls presented to the client and Post from the client? The answer is completely correct.Remaining PostDataIt is indeed the Post data corresponding to the dynamically created control. In step 1 of the Process, Asp.net loads the remaining PostData again after the Page_Load event. This is the secondary loading mentioned in many articles.

For example, a CheckBox is created in the Click Event of a Button and referenced with the member variable CheckBox1. The control Name is CheckBox1. Then it is displayed to the client. The client selects the CheckBox1 and submits it again. Because it is a dynamically created control, when the server processes the submission of the client, Page_Load cannot obtain whether the CheckBox1 is selected. This should be done in the following way: Create the CheckbBox1 again in Page_Load and reference it with the member variable CheckBox1. The control Name is CheckBox1, the object Name must be the same as the Name of the previous dynamically created control), and return, then, in other control events, the check attribute value of the checkbox1 variable is the actual value of the client user operation.

Therefore, the Load event processing method on the Page is the key point to "obtain the PostData of the dynamically created control! Why? This is because only in this Load event can we create a new dynamic control that we created previously. In this case, let the Asp.net Page do "Step 1 and Step 2 ", to load the remaining PostData to the created control object (this is the second loading of Postdata )....

I don't know if the explanation is detailed? If you have any questions, you can find them on MSDN .... I won't find it for you... however, I will provide an example code to explain "how to obtain the PostData data of a dynamically created control". I think it is enough to study it carefully. I don't know anything about the code, you can ask questions at any time! Hope to help you! :-)

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 AspTestForFaq
{
/** // <Summary>
/// Summary description for WebForm6.
/// </Summary>
Public class WebForm6: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. Button CreateCtrl;
Protected System. Web. UI. WebControls. Button Submit;
Protected System. Web. UI. WebControls. Panel Panel1;

Private CheckBox m_CheckBox = null;
Private void Page_Load (object sender, System. EventArgs e)
{
// Put user code to initialize the page here
If (IsPostBack & (null! = ViewState ["Created"])
{
M_CheckBox = new CheckBox ();
Panel1.Controls. Add (m_CheckBox );

}
}

Web Form Designer generated code # region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/** // <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
This. CreateCtrl. Click + = new System. EventHandler (this. CreateCtrl_Click );
This. Submit. Click + = new System. EventHandler (this. Submit_Click );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void CreateCtrl_Click (object sender, System. EventArgs e)
{
If (ViewState ["Created"] = null)
{
M_CheckBox = new CheckBox ();
M_CheckBox.Text = "Click me ";
Panel1.Controls. Add (m_CheckBox );
ViewState ["Created"] = "1 ";
CreateCtrl. Visible = false;
}
}

Private void Submit_Click (object sender, System. EventArgs e)
{
If (m_CheckBox! = Null)
{
If (m_CheckBox.Checked)
M_CheckBox.Text = "Thank you for your check ";
Else
M_CheckBox.Text = "please checked me !!! ";
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.