rohan panchal
rohan panchal

Reputation: 881

Radio button does not return selected value

i am using radio button list control in asp.net. i m trying to get selected value on button click Event but,i m getting Empty string and i want it without javascript.

How can i do this?

<asp:RadioButtonList ID="RadioButtonList1" EnableViewState="true" runat="server" 
    Width="287px">
<asp:ListItem Value="Single" runat="server" Text="Single"></asp:ListItem>
<asp:ListItem Value="Jointly" runat="server" Text="Married Filing Jointly/Widower"></asp:ListItem>
<asp:ListItem Value="Separately" runat="server" Text="Married Filing Separately"></asp:ListItem>
<asp:ListItem Value="Household" runat="server" Text="Head Of Household "></asp:ListItem>
</asp:RadioButtonList>

C# code

protected void btnCalculate_Click(object sender, EventArgs e)
{
    string selectedValue = RadioButtonList1.SelectedValue;
}

Upvotes: 0

Views: 2972

Answers (4)

HatSoft
HatSoft

Reputation: 11191

Remarks from MSDN for RadioButttonList SelectedValue

This property returns the Value property of the selected ListItem. The SelectedValue property is commonly used to determine the value of the selected item in the list control. If multiple items are selected, the value of the selected item with the lowest index is returned. If no item is selected, an empty string ("") is returned.

So suggest #1 is that you atleast make on the item as default selection by using the attribute Selected="true"

Suggestion #2 will be (just a opinion) for the sake of readability use SelectedItem.Value

protected void btnCalculate_Click(object sender, EventArgs e)
{
    string selectedValue = RadioButtonList1.SelectedItem.Value;
}

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

When you bind your RadioButtonList, you can place your code in ! IsPostback , in order to don't erase your `selected value, when you post your control (click event).

Page_Load :

if(! isPostBack)
{
   //Bind your radioButtonList

}`

Nota : You persist your datas with ViewState

Upvotes: 2

Derek
Derek

Reputation: 8630

Or You could use :--

string selectedValue = RadioButtonList1.SelectedValue.ToString();

Upvotes: 0

user1102001
user1102001

Reputation: 707

first check your postback event in your page_load..then you can use RadioButtonList1.SelectedValue

Upvotes: 0

Related Questions