动态创建动态RadioButtonsList
我正在制作一个有趣的“调查”表格,并试图通过使过程过于复杂化来打破单调乏味。我有一组单选按钮列表,我想从一串名称和一串值动态创建它们。价值观不是问题。它正在创建我无法弄清楚的所有单选按钮列表。
例如,我可以继续这样做:
string[] level = {"Expert", "Proficient", "Limited Experience", "No Experience"};
this.rblCSharp.Items.Clear();
for (int i = 0; i < level.GetLength(0); i++) {
this.rblCSharp.Items.Add(level[i]);
}
this.rblCSharp.RepeatDirection = RepeatDirection.Horizontal;
this.rblVbNet.Items.Clear();
for (int i = 0; i < level.GetLength(0); i++)
{
this.rblVbNet.Items.Add(level[i]);
}
this.rblVbNet.RepeatDirection = RepeatDirection.Horizontal;
……但我不想这样做。我想做更多这样的事情:
string[] level = {"Expert", "Proficient", "Limited Experience", "No Experience"};
string[] language = { "CSharp", "VbNet", "VbClassic", "Crystal", "Ssrs", "Sql2005", "UiWeb" };
for (int j = 0; j < language.GetLength(0); j++) {
this.rbl[j].Items.Clear();
for (int i = 0; i < level.GetLength(0); i++) {
this.rbl[j].Items.Add(level[i]);
}
this.rbl[j].RepeatDirection = RepeatDirection.Horizontal;
}
I'm working on a fun 'survey' form and am trying to break up the tedium by over complicating the process. I have a group of radiobuttonlists that I want to dynamically create from a string of names and a string of values. The values is not a problem. It's creating all the radiobuttonlists that I can't figure out.
For instance I could just keep doing this:
string[] level = {"Expert", "Proficient", "Limited Experience", "No Experience"};
this.rblCSharp.Items.Clear();
for (int i = 0; i < level.GetLength(0); i++) {
this.rblCSharp.Items.Add(level[i]);
}
this.rblCSharp.RepeatDirection = RepeatDirection.Horizontal;
this.rblVbNet.Items.Clear();
for (int i = 0; i < level.GetLength(0); i++)
{
this.rblVbNet.Items.Add(level[i]);
}
this.rblVbNet.RepeatDirection = RepeatDirection.Horizontal;
...but I don't want to. I want to do something more like this:
string[] level = {"Expert", "Proficient", "Limited Experience", "No Experience"};
string[] language = { "CSharp", "VbNet", "VbClassic", "Crystal", "Ssrs", "Sql2005", "UiWeb" };
for (int j = 0; j < language.GetLength(0); j++) {
this.rbl[j].Items.Clear();
for (int i = 0; i < level.GetLength(0); i++) {
this.rbl[j].Items.Add(level[i]);
}
this.rbl[j].RepeatDirection = RepeatDirection.Horizontal;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一种方法来引用与语言相对应的 RadioButtonList 控件。您有一个字符串数组,因此一种方法是使用 FindControl 但这不是我的偏好。
另一种方法是:
FindControl 方法(这仅在您动态创建并添加 RadioButtonList 控件时才有意义,而不是在标记中并通过 IntelliSense 提供)
You need a way to reference the RadioButtonList controls that correspond to the languages. You have a string array, so one way would be to use FindControl but that wouldn't be my preference.
Another way would be:
The FindControl approach (this only makes sense if you dynamically created and added the RadioButtonList controls, rather than being available in the markup and via IntelliSense)