Set Radiobuttonlist Selected from Codebehind
Asked Answered
T

5

18

Hey I have a radiobuttonlist and trying to set one of the radiobuttons to selected based on a session variable but proving impossible.

<asp:radiobuttonlist id="radio1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
   <asp:listitem id="option1" runat="server" value="All"/>
   <asp:listitem id="option2" runat="server" value="1" />
   <asp:listitem id="option3" runat="server" value="2" />
</asp:radiobuttonlist> 

I.e How can I set option2 to selected in code behind ?

Ticino answered 14/4, 2011 at 10:51 Comment(1)
listitem does not have the id propertyHast
T
18

You could do:

radio1.SelectedIndex = 1;

But this is the most simple form and would most likely become problematic as your UI grows. Say, for instance, if a team member inserts an item in the RadioButtonList above option2 but doesn't know we use magic numbers in code-behind to select - now the app selects the wrong index!

Maybe you want to look into using FindControl in order to determine the ListItem actually required, by name, and selecting appropriately. For instance:

//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;
Tailband answered 14/4, 2011 at 10:54 Comment(2)
I wonder how it is possible to do a FindControl on a ListItem as it lacks the ID property. Only properties I can recall are Selected, Enabled, Text and Value. Any ideas?Calefacient
listitem does not have the id property therefore I would choose https://mcmap.net/q/649504/-set-radiobuttonlist-selected-from-codebehind (Marquito's answer)Hast
C
20

The best option, in my opinion, is to use the Value property for the ListItem, which is available in the RadioButtonList.

I must remark that ListItem does NOT have an ID property.

So, in your case, to select the second element (option2) that would be:

// SelectedValue expects a string
radio1.SelectedValue = "1"; 

Alternatively, yet in very much the same vein you may supply an int to SelectedIndex.

// SelectedIndex expects an int, and are identified in the same order as they are added to the List starting with 0.
radio1.SelectedIndex = 1; 
Calefacient answered 20/7, 2012 at 15:44 Comment(0)
T
18

You could do:

radio1.SelectedIndex = 1;

But this is the most simple form and would most likely become problematic as your UI grows. Say, for instance, if a team member inserts an item in the RadioButtonList above option2 but doesn't know we use magic numbers in code-behind to select - now the app selects the wrong index!

Maybe you want to look into using FindControl in order to determine the ListItem actually required, by name, and selecting appropriately. For instance:

//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;
Tailband answered 14/4, 2011 at 10:54 Comment(2)
I wonder how it is possible to do a FindControl on a ListItem as it lacks the ID property. Only properties I can recall are Selected, Enabled, Text and Value. Any ideas?Calefacient
listitem does not have the id property therefore I would choose https://mcmap.net/q/649504/-set-radiobuttonlist-selected-from-codebehind (Marquito's answer)Hast
M
15

Try this option:

radio1.Items.FindByValue("1").Selected = true;
Married answered 29/1, 2013 at 18:43 Comment(1)
This solution worked for me while Marquito's didn't for some reason. The reason might be because I had set <asp:ListItem Selected="True" for the first item.Convex
H
5

We can change the item by value, here is the trick:

radio1.ClearSelection();
radio1.Items.FindByValue("1").Selected = true;// 1 is the value of option2
Hermit answered 29/10, 2017 at 17:28 Comment(0)
T
0
var rad_id = document.getElementById('<%=radio_btn_lst.ClientID %>');
var radio = rad_id.getElementsByTagName("input");
radio[0].checked = true;

//this for javascript in asp.net try this in .aspx page

// if you select other radiobutton increase [0] to [1] or [2] like this

Tenure answered 28/1, 2016 at 12:40 Comment(1)
radio_btn_lst : your radiobuttonlist IDTenure

© 2022 - 2024 — McMap. All rights reserved.