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/>")); 
    }