I am adding label and drop down list(not a fixed number of label ,drop down list) dynamically to a form on ASP.NET page in C#, how do I read back data from these controls after post back the page?
Code:-
for (int newNames = 0; newNames < dtDDLBindName.Rows.Count; newNames++)
{
System.Web.UI.HtmlControls.HtmlGenericControl divMapClient = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
divMapClient.ID = 100 + "divMapClient" + newNames;
divMapClient.Attributes.Add("class", "row");
divMapClient.Attributes.Add("runat", "server");
System.Web.UI.HtmlControls.HtmlGenericControl divNewClients = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
divNewClients.ID = 100 + "divNewClients" + newNames;
divNewClients.Attributes.Add("runat", "server");
divNewClients.Attributes.Add("class", "col-sm-6");
Label lblNewClientName = new Label();
lblNewClientName.ID = "lblNewClientName" + newNames;
lblNewClientName.Attributes.Add("runat", "server");
lblNewClientName.Text = dtDDLBindName.Rows[newNames]["Investor Name"].ToString();
divNewClients.Controls.Add(lblNewClientName);
Label lblNewClientID = new Label();
lblNewClientID.ID = "lblNewClientID" + newNames;
lblNewClientID.Attributes.Add("runat", "server");
lblNewClientID.Style.Add("display", "none");
lblNewClientID.Text = dtDDLBindName.Rows[newNames]["Investor Id"].ToString();
divNewClients.Controls.Add(lblNewClientID);
divMapClient.Controls.Add(divNewClients);
divmain.Controls.Add(divMapClient);
System.Web.UI.HtmlControls.HtmlGenericControl br = new System.Web.UI.HtmlControls.HtmlGenericControl("br");
divmain.Controls.Add(br);
}
You can use Control.FindControl that takes id of control as a string and find in this control.
Label l = (Label)ContainerControl.FindControl("ID");
You can assign ids to control in such a way that you can make ids of those controls. For example you create labels with ids lbl1, lbl2 then you can use a loop to get all labels.
for(int i=0 i < lablesCount; i++)
{
Label l = (Label)ContainerControl.FindControl("lbl" + i);
//Your processing goes here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.