[英]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.