Search code examples
c#asp.netloopsradiobuttonlist

How to loop throw asp radiobuttonlist and set specific parameter?


I find this question but it could be deprecated and is not resolve my problem at all. I consider how to loop throw asp:radiobuttonlist which is already bound with data and set visible parameter of specific item. For example I have five items in a radiobuttonlist and I want to make visible only item 1 and item 4.


Solution

  • You could store the items you want to keep, then clear the items and add the stored items:

    int[] keepIndexes = { 0, 3 }; // item 1 and 4
    ListItem[] keepItems = keepIndexes.Select(i => rbl.Items[i]).ToArray();
    rbl.Items.Clear();
    rbl.Items.AddRange(keepItems);
    

    This is the approach if you really want to make them "invisible" since there is no property Visible in ListItem. But maybe you are confusing it with Enabled then this is more appropriate:

    for (int i = 0; i < rbl.Items.Count; i++)
        rbl.Items[i].Enabled = keepIndexes.Contains(i);