Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
3.80/5 (4 votes)
See more:
I have radio buttons in a grid view.
I need to select only one radio button at a time.

I am facing a problem since more than one radio button is getting selected.

I have the following code in script side:
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
                <Columns>
                <asp:BoundField DataField ="Column1" HeaderText ="ProductName"  />
                <asp:BoundField DataField ="Column2" HeaderText ="ProductID" />
                <asp:TemplateField >
                <ItemTemplate>
                <asp:RadioButton  ID ="radioButton1" runat="server" GroupName ="productDB" AutoPostBack ="true" OnCheckedChanged ="Rb_CheckChanged" />
                </ItemTemplate></asp:TemplateField>
                </Columns>
                </asp:GridView>



And in .cs file I have the following code:

C#
foreach (GridViewRow row in GridView1.Rows)
        {
            RadioButton rb1 = (RadioButton)row.Cells[0].FindControl("RadioButton1");
            if (rb1 != null)
            {
                if (rb1.Checked == true)
                {
                    row.BackColor = Color.Yellow;
                    String drugId = (String)row.Cells[1].Text.ToString();
                    }
            }



Let me know what changes has to be made so that only one radio button is selected.
Posted
Updated 19-Apr-11 22:07pm
v2
Comments
Dalek Dave 20-Apr-11 4:07am    
Edited for Grammar and Readability.

You have to Group your all Radio Button With the same Name like below,



<pre lang="xml"><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
                <Columns>
                <asp:BoundField DataField ="Column1" HeaderText ="ProductName"  />
                <asp:BoundField DataField ="Column2" HeaderText ="ProductID" />
                <asp:TemplateField >
                <ItemTemplate>
                <asp:RadioButton  ID ="radioButton1" runat="server" GroupName ="productDB" AutoPostBack ="true" OnCheckedChanged ="Rb1_CheckChanged" />
<asp:RadioButton  ID ="radioButton2" runat="server" GroupName ="productDB" AutoPostBack ="true" OnCheckedChanged ="Rb2_CheckChanged" />
<asp:RadioButton  ID ="radioButton3" runat="server" GroupName ="productDB" AutoPostBack ="true" OnCheckedChanged ="Rb3_CheckChanged" />
                </ItemTemplate></asp:TemplateField>
                </Columns>
                </asp:GridView>


As you can see i have given a Same name to all 3 radio buttons {GroupName =productDB}


I hope this will help you.
 
Share this answer
 
Comments
bsb25 20-Apr-11 3:44am    
this wont work.Since at runtime every radio button will have different grp name
You need to group your radio buttons like advocated in this article: How to group RadioButtons[^].

Best Regards,

-MRB
 
Share this answer
 
Comments
fjdiewornncalwe 19-Apr-11 9:36am    
Exactly...
Manfred Rudolf Bihy 19-Apr-11 9:45am    
Thanks Marcus!
bsb25 20-Apr-11 4:39am    
dont we have a solution without adding any user defined functions or groups.
I wanted a solution which uses inbuilt functions in vs.
Dalek Dave 20-Apr-11 4:07am    
Good Call Manfred
ASP.NET RadioButton control has a property GroupName[^]. All the radio buttons you need to group will have same GroupName.

Hope this helps!
 
Share this answer
 
Comments
bsb25 20-Apr-11 3:44am    
this wont work.Since at runtime every radio button will have different grp name
Ankur\m/ 20-Apr-11 5:21am    
http://www.codeproject.com/KB/webforms/GridViewRadiobuttons.aspx
http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-radio-buttons-vb
http://weblogs.asp.net/abdullaabdelhaq/archive/2009/08/11/radiobutton-inside-gridview-how-to-get-it-work-as-normal.aspx
Or the best,
http://www.google.co.in/#sclient=psy&num=10&hl=en&site=&source=hp&q=radiobutton+inside+gridview&aq=f&aqi=&aql=&oq=&pbx=1&fp=d4343e1d2edcf461
Dalek Dave 20-Apr-11 4:07am    
Nice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900