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.

source share