如何从动态添加标签中检索数据,在asp.net c中动态添加div标签中的下拉列表#

时间:2021-04-09 00:03:08

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);

        }

1 个解决方案

#1


0  

You can use Control.FindControl that takes id of control as a string and find in this control.

您可以使用Control.FindControl将控件的id作为字符串并在此控件中查找。

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.

您可以将ID分配给控件,以便您可以对这些控件进行ID。例如,您使用ids lbl1,lbl2创建标签,然后您可以使用循环来获取所有标签。

for(int i=0 i < lablesCount; i++)
{
  Label l = (Label)ContainerControl.FindControl("lbl" + i);
  //Your processing goes here
}

#1


0  

You can use Control.FindControl that takes id of control as a string and find in this control.

您可以使用Control.FindControl将控件的id作为字符串并在此控件中查找。

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.

您可以将ID分配给控件,以便您可以对这些控件进行ID。例如,您使用ids lbl1,lbl2创建标签,然后您可以使用循环来获取所有标签。

for(int i=0 i < lablesCount; i++)
{
  Label l = (Label)ContainerControl.FindControl("lbl" + i);
  //Your processing goes here
}