4

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>
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
Aparna Singh
  • 95
  • 1
  • 9

5 Answers5

4

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>
INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
  • it is working fine if i add this script in my design code. – Aparna Singh Apr 23 '16 at 08:26
  • i want to add this method in any .js file which is in my project like it is located at "../AllJavaScript/form1.js" – Aparna Singh Apr 23 '16 at 08:27
  • So in that case in script instead of `<%=Docgrid.ClientID%>`, you need to use grid's actual ID which is generated in html. – INDIA IT TECH Apr 23 '16 at 08:30
  • i used that javascript like in my design code and i change my radiobutton tag like but it is not working in the way that is i used.. please give me solution – Aparna Singh Apr 23 '16 at 08:30
  • but how do i know the html generated id? – Aparna Singh Apr 23 '16 at 08:31
  • Check your html view source, and look at javascript function. Instead of `document.getElementById("<%=Docgrid.ClientID%>");`, actual grid's id used. So same way you need to change this line. – INDIA IT TECH Apr 23 '16 at 08:33
  • Right click on browser, and click on view source. – INDIA IT TECH Apr 23 '16 at 08:35
  • True, that's the reason its not preferral to use .js file for this kind of scripts. But still if you want to use js, use hardcoded grid's clientID in script. – INDIA IT TECH Apr 23 '16 at 08:38
  • I don't think it is going to change every time. Its true that it will generated once like 'ctl0_gridview1', but that will remain same when you page next time. Its not going to generate different id each time. It will remain same which is generated first time. Please check and let me know. – INDIA IT TECH Apr 23 '16 at 08:50
0

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>
0

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.

Community
  • 1
  • 1
Geraldo Neto
  • 3,670
  • 1
  • 30
  • 33
0

Try this-

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

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
0

Simplest way to solve this problem is in property window for all radio buttons set the GroupName as any name. in my case I set GroupName as "A". set that same GroupName too all Radio Buttons Property. then run the code :)

I hope this might help.