views:

159

answers:

1

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

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;
}
Ahmad Mageed
Worked perfectly! You gave me just enough information to have to do a bit a research to get it working. Thanks!I thought about using FindControl to get all the IDs for the radiobuttonlists instead of having a separate list in the code behind. But thought that would be more than I could handle at this point.
Mikecancook
Cool, glad I could help. I just updated my post with the FindControl approach as well.
Ahmad Mageed