如何让datalist里的radiobutton是单选状态

img


每个radiobutton控件我已经绑定了后台数据,想去radiobutton_checkchanged里写但是也联想不出radiobutton😭

麻烦采纳一下,谢谢啦


为了让Datalist中的RadioButton是单选状态,你需要做以下几件事:

把RadioButton放在同一个RadioButtonList控件里
xml
Copy
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Vertical">
    <asp:ListItem Value="1">Option 1</asp:ListItem>
    <asp:ListItem Value="2">Option 2</asp:ListItem>
    <asp:ListItem Value="3">Option 3</asp:ListItem>
</asp:RadioButtonList>
为RadioButtonList设置GroupName,这样RadioButton才能成为一组
xml
Copy
<asp:RadioButtonList ID="RadioButtonList1" 
    GroupName=" Foo"
    runat="server" RepeatDirection="Vertical">
       ...
</asp:RadioButtonList>
在Datasource读取数据时,将RadioButtonList绑定到Datasource
xml
Copy
<asp:RadioButtonList ID="RadioButtonList1" 
   DataSourceID="DataSource1"    
   runat="server" RepeatDirection="Vertical">
</asp:RadioButtonList>

<asp:SqlDataSource ID="DataSource1" runat="server" ...>
</asp:SqlDataSource>  
在选中某个RadioButton时,清空其他RadioButton的Selected属性
csharp
Copy
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList rbl = sender as RadioButtonList;
    
    for(int i = 0; i < rbl.Items.Count; i++) {
        rbl.Items[i].Selected = false;    
    }
    rbl.SelectedValue = rbl.SelectedItem.Value;  
}
以上这些设置就可以让Datasource绑定的RadioButton成为单选状态组了。

希望这有助于解决你的问题!请让我知道如果你还有任何其他疑问。