I am building many textboxes programmatically in my ASP.NET page, after I’ve clicked a button, I would like to process those values, is there any possibility to retrieve them from their ID in ViewState ?
Here is my code :
Reference of the table in the aspx :
<asp:Table ID="Distances" runat="server" ViewStateMode="Inherit"></asp:Table>
Then in code behind after creating all the rows and cells, I add a textbox into some of them :
Distances.Rows[j].Cells[i].Controls.Add(CreateTB(distance.ToString(), (i + j * rows).ToString(), false));
protected TextBox CreateTB(string text, string id, bool ebanled = true)
{
TextBox tb = new TextBox() { Text = text, ID = id, Enabled = ebanled};
tb.TextChanged += new EventHandler(OnTBChanged);
return tb;
}
ViewStateis enabled by default, so it should already work that theTextproperty is persisted across postbacks.So you could for example use
FindControl("TextBoxID")or enumerate them to get the refernce to theTextBox(assuming that they are added to a container-control likePanel):or
I assume you’re not recreating those
TextBoxeson postbacks. Therefore you need to use the same ID as before and recreate them inPage_Loadat the latest stage in page’s life-cycle. So you can create them in an event, but you cannot recreate them there.You should show your code where you create them dynamically, then i could be more specific.
TRULY UNDERSTANDING DYNAMIC CONTROLS