[英]how to retrieve data from dynamically added label,drop down list in dynamically added div tag in asp.net c#
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? 我将标签和下拉列表(不是固定数量的标签,下拉列表)动态添加到C#的ASP.NET页面上的表单中,在发回页面后如何从这些控件中读取数据?
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. 您可以使用将控件ID作为字符串的Control.FindControl ,并在此控件中查找。
Label l = (Label)ContainerControl.FindControl("ID");
You can assign ids to control in such a way that you can make ids of those controls. 您可以通过为控件分配ID的方式来制作这些控件的ID。 For example you create labels with ids lbl1, lbl2 then you can use a loop to get all labels.
例如,您创建的ID为lbl1,lbl2的标签,然后可以使用循环获取所有标签。
for(int i=0 i < lablesCount; i++)
{
Label l = (Label)ContainerControl.FindControl("lbl" + i);
//Your processing goes here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.