ASP.NET textboxes hold their text property after post back
on the msdn we have this http://msdn.microsoft.com/en-us/library/ms972976.aspx. Generally the life of a web site looks like this: Initialization->LoadViewState->LoadPostBackData->Load->RaisePostBackEvent->SaveViewState->Render
I have a placeholder on my aspx side it looks like this:
<asp:PlaceHolder ID="ph1" runat="server">
<asp:Button OnClick="ClickMe" ID="Button1" runat="server" Text="Button" />
</asp:PlaceHolder>
I'm creating two textboxes on the Page Load event the code looks like this:
for (int i = 0; i <2; i++)
{
TextBox tb = new TextBox();
tb.ID = "tb" + i.ToString();
tb.Text = "my test string";
ph1.Controls.Add(tb);
}
And the click button event looks like this:
protected void ClickMe(object sender, EventArgs e)
{
var mycontrols = ph1.Controls;
}
When I put sommething in to those textboxes and click the button the page is postedback but my textboxes hold the inputed values despite the fact that I'm changing their text property on the page Load event. Those controls are created dynamically on the Load event which is fired after the LoadViewState and LoadPostBackD开发者_如何学Goata so how does it work?? When do controls receive their postdata??
Your premise of the order of events is slightly wrong. See this page: http://msdn.microsoft.com/en-us/library/aa479328.aspx. You'll find that the ProcessPostData actually occurs twice, once before PageLoad,and once after to handle dynamically created controls that weren't present the first time.
Also, it is a common misconception that this has something to do with ViewState. It does not. Try setting EnableViewState=false on your textboxes and see if it makes a difference.
精彩评论