Is it possible to hide a particular list item of radiobuttonlist programmatically?
Asked Answered
B

6

12

I have used gridview in my aspx page.. In that I have a list with six radio buttons in a single cell aligned horizontally.

I need the 1st radio button to be hidden. How to achieve that programmatically?

<asp:GridView ID="CrowdRatingGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="4" OnPageIndexChanging="CrowdRatingGrid_PageIndexChanging" ViewStateMode="Enabled">

<PagerSettings Mode="Numeric" PageButtonCount="4" />
<Columns>
<asp:TemplateField>
   <HeaderTemplate>
    Rating
    </HeaderTemplate>
    <ItemTemplate>
     <asp:RadioButtonList runat="server" ID="Rating" SelectedValue='<%# Bind("rating_id") %>'
                            RepeatDirection="Horizontal">
                            <asp:ListItem Value="0" />
                            <asp:ListItem Value="1" />
                            <asp:ListItem Value="2" />
                            <asp:ListItem Value="3" />
                            <asp:ListItem Value="4" />
                            <asp:ListItem Value="5" />
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
         </Columns>
        </asp:GridView>

I need the List Item with value=0 to be hidden. How would I achieve this?

Bostick answered 3/4, 2013 at 15:45 Comment(1)
If you need different logic for specific items don't use a RadioButtonList but single RadioButtons. You can use a container control like Panel and appropriate CSS for the layout. Use the GroupName when only one selection is possible from a list of available options.Peugia
V
15

Yes, you can hide one by setting its Enabled property to false:

Rating.Items[0].Enabled = false;

Editing based on comment by OP. To completely get rid of it you'll need to do this:

Rating.Items.RemoveAt(0);

and then when you want it back you'll need to do this:

Rating.Items.Insert(0, "0");
Virtues answered 3/4, 2013 at 15:58 Comment(2)
I need it to be hidden... your code just disables the radiobuttonBostick
@Coder, to do that you'll need to Remove it then, and Insert it back in when relevant.Virtues
O
4

EDIT: Ok, I dug into this a little deeper and see how .NET renders the RadioButtonList control when you set it to Horizontal (it's a table with <td>'s for each item).

Try this:

var bli = Rating.Items[ 0 ];
bli.Attributes.Add( "hidden", "hidden" );
Orthopedist answered 3/4, 2013 at 16:17 Comment(4)
I need it to be hidden... your code just disables the radiobuttonBostick
Can you explain the difference you're seeing between hiding and disabling the radio button?Orthopedist
hiding means the radibutton will not be visible in screen... Disabling means it will be visible but cannot be checkedHumiliation
@Humiliation - understood, I wanted to be sure that what I was seeing was the same as what Coder was seeing. Sometimes hidden isn't always hidden in the asp.net world... I updated my answer after realizing that my suggestion wouldn't work for what Coder wanted to do.Orthopedist
L
0

It's working fine.

RadioButtonList.Items[index].Enable=True/False;

Ex:

rdTypePackage.Items[1].Enabled = false;
Liquefy answered 20/12, 2013 at 7:13 Comment(1)
This will not hide the ListItem, it will only appear as disabled on the page.Ruckman
M
0
radioListID.Items[index].Enabled = true/false;

It will work 100%.

Mcculloch answered 18/3, 2015 at 10:49 Comment(1)
This will not hide the ListItem, it will only appear as disabled on the page.Ruckman
F
0

Old thread, though to achieve this you can do:

//Ensure the item is disabled    
rblRating.Items[0].Enabled = false;
//Ensure the item is not selected
rblRating.Items[0].Selected = false;
//next row of code is hiding your radio button list item through css
rblRating.Items[0].Attributes[HtmlTextWriterStyle.Visibility] = "hidden";
Fortna answered 12/5, 2017 at 8:31 Comment(0)
R
0
ListItem li = Rating.Items(0);
Rating.Items.Remove(li);
Ruckman answered 10/1, 2020 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.