C# radiobutton 分组取值

来源:百度知道 编辑:UC知道 时间:2024/09/23 19:10:24
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Sl" />

<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Sl" />

<asp:RadioButton ID="RadioButton3" runat="server" GroupName="Sl" />

<asp:RadioButton ID="RadioButton4" runat="server" GroupName="Sl" />

这样组合起来的radiobutton 在后台怎么来取哪一个被选中呢?
我页面中的radiobutton太多了 所以才想在后台有没的简单的方法, 我试过radiobuttonlist ,不过那个的布局很差

暂分为2种情况
首先如果你的页面无其他刷新
则将上述按钮的autopostback设置为true
再给他们赋予同一个checkedchange事件
protected void checkedchange(object sender, EventArgs e)
{
RadioButton ran = (RadioButton)sender;
if (ran.Checked == true)
{
//返回选中的ran.ID;
}
}
另一种情况,是页面有其他刷新,则把autopostback设置为false
同时需要在页面其他控件,如按钮来提供刷新,提交到服务器checkedchange代码不变.
这样比较麻烦
建议使用RadioButtonList控件
通过RadioButtonList1.Item[i].Selected属性来判断是否被选中

既然如此,我觉得你就用我的方法就可以了,一个组的按钮用一个事件,再好的办法或许有,但是我不知道了,不好意思了。希望对你有所帮助

用if和else来判断
如果RadioButton 被选中了
就等于RadioButton
else if(RadioButton2被选中了)
就等于RadioButton2

可以通过判断RadioButton的checked属性来判断是否被选中.
如果认为麻烦了可以用RadioButtonList控件.
通过RadioButtonList1.Item[i].Selected属性来判断是否被选中.

radiobuttonX.checked