gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:26 25 4
gpt4 key购买 nike

我在 C# 中将标签和下拉列表(不是固定数量的标签、下拉列表)动态添加到 ASP.NET 页面上的表单,如何在回发页面后从这些控件中读回数据?

代码:-

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

}

最佳答案

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

Label l = (Label)ContainerControl.FindControl("ID");

您可以将 id 分配给控件,这样您就可以创建这些控件的 id。例如,您创建 ID 为 lbl1、lbl2 的标签,然后您可以使用循环获取所有标签。

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

关于c# - 如何从动态添加的标签中检索数据,在asp.net c#中动态添加的div标签中的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com