2012-09-27 33 views
0

我正在使用asp.net中的单选按钮列表控件。 我试图获得选定的值按钮点击事件 但我越来越Empty string,我希望它没有JavaScript。单选按钮不返回选定的值

我该怎么做?

<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#代码

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

请告诉我的代码 – HatSoft

+1

你能后你有这么远的代码? –

+0

我已编辑我的问题 –

回答

2

当您绑定的单选按钮列表,你可以将你的代码! IsPostback,为了不erase你`选择的值,当你发布你的控制(点击事件) 。

的Page_Load:

if(! isPostBack) 
{ 
    //Bind your radioButtonList 

}` 

诺塔:你坚持你的DATAS与ViewState

0

从MSDN备注为RadioButttonList的SelectedValue

此属性返回选定的ListItem的Value属性。 SelectedValue属性通常用于确定列表控件中所选项目的值。如果选择多个项目,则返回具有最低索引的所选项目的 值。 如果没有选择 项目,则返回空字符串(“”)。

所以建议#1是你ATLEAST做的项目作为默认选择使用属性选择=“真正的”

建议#2将是(只是一个意见)的可读性的原因使用SelectedItem.Value

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

首先检查你的page_load..then回传事件中,你可以使用RadioButtonList1.SelectedValue

0

或者你可以使用: -

string selectedValue = RadioButtonList1.SelectedValue.ToString();