how to prevent radiobutton for multiple selection?

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

i am using radio button in my grid view control and now i want to allow user to select only one radio button in grid view but i am unable to do that.below is my code in design.please help me.

<asp:GridView ID="Docgrid" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"                      runat="server" AutoGenerateColumns="false" BorderStyle="None" GridLines="None">                 <Columns>                     <asp:TemplateField>                         <ItemTemplate>                             <asp:RadioButton ID="chkRow" GroupName='docid' runat="server" />                         </ItemTemplate>                     </asp:TemplateField>                     <asp:BoundField DataField="Id" HeaderText="DocId" ItemStyle-Width="50" />                     <asp:BoundField DataField="DocName" HeaderText="DocName" ItemStyle-Width="50" />                     <asp:BoundField DataField="DocType" HeaderText="DocType" ItemStyle-Width="50" />                     <asp:BoundField DataField="Size" HeaderText="Size" ItemStyle-Width="50" />                     <asp:BoundField DataField="UploadedBy" HeaderText="UploadedBy" ItemStyle-Width="50" />                     <asp:BoundField DataField="Status" HeaderText="Status" ItemStyle-Width="50" />                     <asp:TemplateField HeaderText="UpdatedBy" ItemStyle-Width="50">                     <ItemTemplate>                         <asp:Label ID="UpdatedBy" runat="server" Text='<%# Eval("UpdatedBy") %>'></asp:Label>                     </ItemTemplate>                     </asp:TemplateField>                 </Columns>              </asp:GridView>

回答1:

Try below,

Change your radio button as below,

<asp:RadioButton ID="chkRow" GroupName='docid' runat="server" onclick = "RadioCheck(this);" />

Add below javascript,

<script type = "text/javascript">      function RadioCheck(rb) {         var gv = document.getElementById("<%=Docgrid.ClientID%>");         var rbs = gv.getElementsByTagName("input");          var row = rb.parentNode.parentNode;         for (var i = 0; i < rbs.length; i++) {             if (rbs[i].type == "radio") {                 if (rbs[i].checked && rbs[i] != rb) {                     rbs[i].checked = false;                     break;                 }             }         }     }     </script>


回答2:

here you can chuse one of the item using Radiobuttonlist

<asp:RadioButtonList ID="RadioButtonList1" runat="server">                 <asp:ListItem>Option 1</asp:ListItem>                 <asp:ListItem>Option 2</asp:ListItem>                 <asp:ListItem>Option 3</asp:ListItem>             </asp:RadioButtonList>


回答3:

Put all radio buttons for a group in a container object like a Panel or a GroupBox. As suggested here.

Usually a radiobutton only accepts one selection by default, when its grouped. But if you still have the problem, you can use a boolean control variable (e.g bool isAnySelected) and implement a listener for your radio button's check. When one of them is selected, look for the variable, if the variable is false make the variable true. If the variable is already true, uncheck the radio button.



回答4:

Try this-

new { @id = kk } should be new { @id = kk, Name = "grp" } to prevent multiple selection of Radio Button.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!