打開冰箱 2023-06-14 12:20 采纳率: 0%
浏览 8

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

img


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

  • 写回答

1条回答 默认 最新

  • 火花怪怪 2023-06-14 16:44
    关注

    麻烦采纳一下,谢谢啦

    
    为了让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成为单选状态组了。
    
    希望这有助于解决你的问题!请让我知道如果你还有任何其他疑问。
    
    评论

报告相同问题?

问题事件

  • 创建了问题 6月14日

悬赏问题

  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊
  • ¥15 快速扫描算法求解Eikonal方程咨询
  • ¥15 校内二手商品转让网站
  • ¥20 高德地图聚合图层MarkerCluster聚合多个点,但是ClusterData只有其中部分数据,原因应该是有经纬度重合的地方点,现在我想让ClusterData显示所有点的信息,如何实现?