Search code examples
c#asp.netdynamicradiobuttonlist

Dynamically created dynamic RadioButtonsList


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;
    }

Solution

  • 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;
    }