动态创建动态RadioButtonsList

发布于 2024-08-04 11:41:42 字数 1167 浏览 7 评论 0原文

我正在制作一个有趣的“调查”表格,并试图通过使过程过于复杂化来打破单调乏味。我有一组单选按钮列表,我想从一串名称和一串值动态创建它们。价值观不是问题。它正在创建我无法弄清楚的所有单选按钮列表。

例如,我可以继续这样做:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2024-08-11 11:41:42

您需要一种方法来引用与语言相对应的 RadioButtonList 控件。您有一个字符串数组,因此一种方法是使用 FindControl 但这不是我的偏好。

另一种方法是:

var languageControls = new List<RadioButtonList> { rblCSharp, rblVbNet, rblVbClassic, rblCrystal, rblSsrs, rblSql2005, rblUiWeb };

foreach(var rbl in languageControls)
{
    rbl.Items.Clear();
    // this could be a foreach instead, but I kept your original code
    for (int i = 0; i < level.GetLength(0); i++)
    {
        rbl.Items.Add(level[i]);
    }
    rbl.RepeatDirection = RepeatDirection.Horizontal;
}

FindControl 方法(这仅在您动态创建并添加 RadioButtonList 控件时才有意义,而不是在标记中并通过 IntelliSense 提供)

string[] level = {"Expert", "Proficient", "Limited Experience", "No Experience"};
string[] language = { "CSharp", "VbNet", "VbClassic", "Crystal", "Ssrs", "Sql2005", "UiWeb" };

foreach (string lang in language)
{
    RadioButtonList rbl = (RadioButtonList)FindControl("rbl" + lang);
    if (rbl == null)
        continue;  // keep going through the rest, or throw exception

    rbl.Items.Clear();
    // this could be a foreach instead, but I kept your original code
    for (int i = 0; i < level.GetLength(0); i++)
    {
        rbl.Items.Add(level[i]);
    }
    rbl.RepeatDirection = RepeatDirection.Horizontal;
}

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:

var languageControls = new List<RadioButtonList> { rblCSharp, rblVbNet, rblVbClassic, rblCrystal, rblSsrs, rblSql2005, rblUiWeb };

foreach(var rbl in languageControls)
{
    rbl.Items.Clear();
    // this could be a foreach instead, but I kept your original code
    for (int i = 0; i < level.GetLength(0); i++)
    {
        rbl.Items.Add(level[i]);
    }
    rbl.RepeatDirection = RepeatDirection.Horizontal;
}

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)

string[] level = {"Expert", "Proficient", "Limited Experience", "No Experience"};
string[] language = { "CSharp", "VbNet", "VbClassic", "Crystal", "Ssrs", "Sql2005", "UiWeb" };

foreach (string lang in language)
{
    RadioButtonList rbl = (RadioButtonList)FindControl("rbl" + lang);
    if (rbl == null)
        continue;  // keep going through the rest, or throw exception

    rbl.Items.Clear();
    // this could be a foreach instead, but I kept your original code
    for (int i = 0; i < level.GetLength(0); i++)
    {
        rbl.Items.Add(level[i]);
    }
    rbl.RepeatDirection = RepeatDirection.Horizontal;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文