1

我想给我的动态生成的复选框通过一个for循环算法的名称。使用算法很简单,但我无法给他们名称/复选框文本。我能想到的最好的方法是生成一切的类似复选框文本。有没有办法来命名它们?如何动态地给自定义的文本/命名约定动态生成标签/复选框?

下面是我想出了

int x = ((((DropDownList1.SelectedIndex+1)*(DropDownList1.SelectedIndex+1))-(DropDownList1.SelectedIndex+1))/2)-1; 
    Label1.Text = x+""; 
    for (int i = 0; i < x; i++) 
    { 
     CheckBox cb = new CheckBox(); 
     cb.Text = "1 & 2"; 
     PlaceHolder1.Controls.Add(cb); 
     PlaceHolder1.Controls.Add(new LiteralControl("<br/>")); 
    } 

任何帮助的代码是极大的赞赏。

干杯, JAF

回答

0

您应该指定使用控件的属性ID的名称。

像这样:

for (int i = 0; i < x; i++) 
{ 
    CheckBox cb = new CheckBox(); 

    cb.ID = "checkBox" + i; 

    PlaceHolder1.Controls.Add(cb); 

    PlaceHolder1.Controls.Add(new LiteralControl("<br/>")); 
} 

Text属性可以在每个控制等。
控件的名称(ID)不能相同。

0

您可以使用for语句指数:

int x = ((((DropDownList1.SelectedIndex+1)*(DropDownList1.SelectedIndex+1))-(DropDownList1.SelectedIndex+1))/2)-1; 
    Label1.Text = x+""; 
    for (int i = 0; i < x; i++) 
    { 
     CheckBox cb = new CheckBox(); 
     cb.Text = String.Format("PrefixName{0}" , Convert.ToString(i+1)); 
     PlaceHolder1.Controls.Add(cb); 
     PlaceHolder1.Controls.Add(new LiteralControl("<br/>")); 
    }