错误为“具有相同ID的多个控件"当我动态创建行时 [英] getting an error as " multiple controls with the same ID" when im creating rows dynamically

查看:55
本文介绍了错误为“具有相同ID的多个控件"当我动态创建行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每个butn click事件,我想在表中添加一个新行
行数在count变量= 0中;

For every butn click event i want add a new row to the table
the count of rows are in the count variable=0;

Table Table1= new Table();
if(count>=0)
{
  if(count==0)
  {
  TableRow tr= new TableRow();
  tr.ID= "tr"+count;//shall i use the this stmt to identify the each row individually
 //for first row
 //code for as header
  }
 int column=0;
 for(column=0;column<=3;column++)
 {
   TableCell tc=new TableCell();
   tc.ID= "tc"+count+""+column;//same as the row
   switch (column)
   {
     case 1:
           Label lb=new label();
           tc.Controls.Add(lb);
           lb.ID="label" +count+""+column;
           lb.Text="label"+count+""+column;
           tc.Controls.Add(lb);
           break;
     case 2:
           TextBox tb=new TableBox();
           tb.ID= "txt" +count+""+column;
           tc.Controls.Add(tb);break;
     case 3:
           DropDownList ddl=new DropDownList();
           ddl.ID="ddl"+count+""+column;
           tc.Controls.Add(ddl);break;
    }
    tr.Controls.Add(tc);
}
count++;
table1.Controls.Add(tr);

}


编码之后,是否有可能出现任何错误?

[edit]已添加代码块-OriginalGriff [/edit]


After this coding Is there any possibility of getting any error?

[edit]Code block added - OriginalGriff[/edit]

推荐答案

我的猜测是您正在将调用此代码的代码的count设置为0.第一次起作用吗?

为什么只在计数为0时才添加新行?这对我来说毫无意义.

每次调用此代码段时,您还将创建一个全新的表,并且该表中的元素始终具有相同的名称,因此,如果两次调用它,则会得到两个ID冲突的表.那与您的文字描述不符,因此我怀疑您不想每次都创建一个新表,而是想将一个表传递给此方法.然后,您可以使用table.Controls.Count或类似方法获取可用于id的行计数,而不是依赖于count变量.
My guess is that you''re setting count to 0 in the code that calls this. Does it work the first time?

Why do you only add a new row if the count is 0? That makes no sense to me.

You are also creating a whole new table each time this snippet is called, and the elements within that table always have the same name, so if you call it twice you get two tables with clashing IDs. That doesn''t match what your text description is, so I suspect you don''t want to make a new table each time, but instead you want to pass a table into this method. You can then use table.Controls.Count or similar to get a row count that you can use for an id, instead of relying on the count variable.


您必须更改计数值在For循环退出后.
You have to change the count value after the For loop exits.


在count内放入for循环...........
put count++ inside for loop..........


这篇关于错误为“具有相同ID的多个控件"当我动态创建行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆