如何动态地为动态生成的标签/复选框提供自定义文本/命名约定?
我试图通过 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/>"));
}
非常感谢任何帮助。
干杯, 贾夫
I am trying to give names to my dynamically generated checkboxes through a for loop an algorithm. Coming up with the algorithm was pretty simple but I am unable to give them name/checkbox text. The best I could come up with was a similar checkbox text for everything that was generated. Is there a way to name them?
Below is the code I came up with
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/>"));
}
Any help is greatly appreciated.
Cheers,
Jaf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 for 语句的索引:
You can use index of for statement:
您应该使用控件的
ID
属性来指定名称。像这样:
每个控件中的
Text
属性都可以相同。控件的名称 (ID) 不能相同。
You should assign the name using the control's
ID
property.Like this:
The
Text
property can be equal in every control.You can't have identical names (IDs) for controls.