Why do my dynamically added controls lose their values ​​after Postback?

To ask my question, I created an aspx file containing Button and DataList with SqlDataSource :

  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:DataList ID="DataList1" runat="server" DataKeyField="a" DataSourceID="SqlDataSource1" > <ItemTemplate> a: <asp:Label ID="aLabel" runat="server" Text='<%# Eval("a") %>' /> <br /> b: <asp:Label ID="bLabel" runat="server" Text='<%# Eval("b") %>' /> <br /> </ItemTemplate> </asp:DataList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:probaConnectionString %>" SelectCommand="SELECT [a], [b] FROM [PROBA_TABLE]"></asp:SqlDataSource> 

In my code behind, I am adding TextBoxes to the DataList elements. I am adding a TextBox to Page_Load to each element, and another TextBox to Button Click eventhandler .

  public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { foreach (DataListItem item in DataList1.Items) { item.Controls.Add(new TextBox()); } } } protected void Button1_Click(object sender, EventArgs e) { foreach (DataListItem item in DataList1.Items) { item.Controls.Add(new TextBox()); } } } } 

This works just fine, except for one. When I click the button, the text fields that were created in Page_Load retain their Text value, but the text fields that were created in Button1_Click lose their Text values. My real problem is more complicated than this, but I think that solving this will help me a lot.

My site after postback

+4
source share
2 answers

Each control that should receive data from the ViewState page must be created in the Init or Load event handlers, since the ViewState is saved for the BEFORE BEGINNING controls , / strong> and other control events (these events are triggered when changes to the ViewState are detected, so ViewState must be read before running the Click event).

So the process should look like this:

  • OnInit (static controls are created)
  • Static Control Content Deserialized from ViewState
  • OnLoad (create dynamic controls in your text files created in the last callback)
  • Dynamic Management Content Deserialized from ViewState
  • Click, change , and other events will be triggered according to the detected changes comparing the POST data and the ViewState data.

Suggestions:

You can use hidden fields to store additional state information, and then in OnLoad you can read this information to recreate dynamically created controls.

In addition, you must explicitly specify the identifier property of your text fields so that values ​​can be stored correctly, do not rely on ASP.Net.

+3
source

http is stateless by default, which means that after processing your request, the server does not save the data or information about the request
but the values ​​in the form should be saved in special cases when there is an error

Suppose you fill out a long form and then send it back to the server to receive an error message, and all the completed values ​​disappear. it will not be annoying

so asp.net does behind the scenes that it stores a line in a hidden page that has information about all the server controls and their identifiers, so when you submit the form back, the Page class and values ​​are sent back and bound to certain controls, because that each request generates a Page class event, pageLoad , and the controls created in PageLoad then present the current values ​​corresponding to their identifiers, in contrast to the controls that are created when the button is pressed, until the button_click event is button_click , which has already been analyzed, and the values ​​are filled with them

0
source

All Articles