【发布时间】:2014-08-13 07:57:26
【问题描述】:
我通过让用户输入所需的行数,将控件动态添加到 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]);
现在成功添加行后,当我刷新页面并再次尝试 添加新行,它给了我相同的 ID 'nomenclature0' 错误。
请帮我解决这个问题,
提前致谢,
【问题讨论】:
-
你可以在你的问题中添加完整的错误吗?
-
我在猜测解决方案 - 最初,当 i=0 时,ID 为“Nomenclature0”。请检查您是否没有增加 i 的值,您将拥有相同的 i 值并且 ID 将为 Nomenclature0。
-
@KrunalPatil:完全错误:找到具有相同 ID 'nomenclature0' 的多个控件。 FindControl 要求控件具有唯一 ID