繁体   English   中英

向ASP.NET中的表添加动态控件时出现相同的ID错误

[英]Same ID error while adding dynamic controls to table in ASP .NET

通过让用户输入所需的行数,我将控件动态添加到ASP.NET中的表。 addRow()函数的示例代码为:

HtmlTableCell cell_1 = new HtmlTableCell();
HtmlTableCell cell_2 = new HtmlTableCell();
l = new Label();
nomenclature = new TextBox();
unit = new TextBox();

row[i] = new HtmlTableRow();

label.Text = "" +(i+ 1);           
cell_1.Controls.Add(l);
row[i].Controls.Add(cell_1);

nomenclature.TextMode = TextBoxMode.MultiLine;
nomenclature.Rows = 5;
nomenclature.Columns = 40;
nomenclature.ID="nomenclature"+i;
nomenclature.Text = "NA";
cell_2.Controls.Add(nomenclature);
row[i].Controls.Add(cell_2);

table_items.Controls.Add(row[i]);

现在成功添加行,当我刷新页面,然后再次尝试添加新行,它给了我sameID“nomenclature0”的错误。

请帮助我

提前致谢,

我没有关于您的代码的足够信息。

两种可能的解决方案:

1)试试这个“ table_items.Controls.Clear();” 在调用addRows函数之前。

2)如果是ispostback,请不要填写表格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM