Abdulla AbdelHaq Blog


ForEach(Minute in MyLife)
MyExperience ++;

RadioButton inside GridView, How to get it work as normal.

As we all know, RadioButton control used in a situation when you want end-users to select only one option at a time from group of options.

Did you tried before to drag a RadioButton control inside a Gridivew templatefield, and then you attempt to select these RadioButtons , you will notice that the behavior of RadioButton control will be changed and it will work just like the behavior of checkbox control! the user will be able to select more than one radiobutton in the grid!

O.k you can use html radiobutton element instead of server radiobutton control in the templatefield, this will do the trick and the behavior will get back again, but what if you want to do some event in codebeind and collect the selected Radiobutton?! this time you need to add the run at server attribute to the html element to be able to see it in the codebehind, in that case, the behavoir will be changed again and the user can select more than one option from the radio buttons.

So what is the solution for that ?

In simplest scenario, when the situation is only radio button and text beside it, you can use the RadioButtonList control instead, but if the form gets to be more complex and you need to display other controls beside the radiobutton, then (gridview,repeater,datalist) should be your choice.

the below code will help you to accomplish that.

<script type="text/javascript" language="javascript"> 

     

 function fnCheckUnCheck(objId) 

   {

  var grd = document.getElementById("<%= GridView1.ClientID %>"); 

      

   //Collect A

   var rdoArray = grd.getElementsByTagName("input"); 

       

   for(i=0;i<=rdoArray.length-1;i++) 

    { 

     if(rdoArray[i].type == 'radio')

      {

       if(rdoArray[i].id != objId) 

        { 

           rdoArray[i].checked = false; 

        } 

     }

   } 

} 

</script>

<asp:GridView ID="GridView1" runat="server"

 AutoGenerateColumns="false" >

<Columns>

 

<asp:TemplateField HeaderText="Select/UnSelect"> 

<ItemTemplate > 

<input id="grdRdo" name="grdRdo" type="radio" runat="server"

  onclick="fnCheckUnCheck(this.id);" /> 

 </ItemTemplate>

 </asp:TemplateField>

 

<asp:BoundField DataField="User_Name" HeaderText="Name" />

<asp:BoundField DataField="User_Department" HeaderText="Dep" />

   

</Columns>

</asp:GridView>

 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

 

  For Each grdRow As GridViewRow In GridView1.Rows

    Dim htmlRdo As New HtmlInputRadioButton

   htmlRdo = DirectCast(grdRow.FindControl("grdRdo"), HtmlInputRadioButton)

 

     If htmlRdo.Checked Then

         ' Do Something

     Else

         'Do something else

      End If

 

   Next

 

End Sub

I hope someone will found this very helpful :)

~ Abdulla AbdelHaq

Comments

RadioButton inside GridView, How to get it work as normal. | ASP Scribe said:

Pingback from  RadioButton inside GridView, How to get it work as normal. | ASP Scribe

# August 11, 2009 4:27 AM

WebDevVote.com said:

You are voted (great) - Trackback from WebDevVote.com

# August 11, 2009 5:04 AM

AndrewSeven said:

Its better to fix the behavior of the control so that the group name works than to add script each time you use it.

What you need to change is the behavior of RenderInputTag(HtmlTextWriter writer, string clientID, string onClick)

so that instead of this

  writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueGroupName);

you have this

  writer.AddAttribute(HtmlTextWriterAttribute.Name, this.GroupName);

unfortunately the method is internal, so you would have to override Render and duplicate the code in the CheckBox's Render and in the RenderInputTag method.

# August 11, 2009 10:57 AM

Abdulla.Abdelhaq said:

Thanks Andrew, it is really nice way to think for solving this :)

Actually you do not need to write my javascript code in every page, you can simple put it inside external Javascript file and then you can add later on as reference.

Your solution was to create a new custom control, where in fact you can solve it too using few simple javascript line of code :)

# August 12, 2009 2:38 AM

Manickaraj said:

Hi i'm Manickaraj, previously i worked in JAVA platform. I am beginner in asp.net, Very very useful this code. So many Thanks U.....

# August 26, 2009 8:08 AM

satish said:

hi,

 this is good...

 great job man ...

# August 27, 2009 2:53 AM

ghassan_aljabiri said:

hello abdullah

i used that script

but unfortunatily i have a runtime error

------------------

Microsoft JScript runtime error: 'null' is null or not an object

--------------

this error happens when interpreting

var rdoArray = grd.getElementsByTagName("input");

NOTE:

i use listview instead of Gridview

Any Ideas

# October 26, 2009 11:03 AM

LakshmiNarayan said:

Very helpful..

Thanks a lot :)

# October 30, 2009 6:43 AM

ghassan_aljabiri said:

the erro was because of using an the listview inside an update panel

its resolved now , thanks alot

# November 6, 2009 12:48 PM

Adwait Kulkarni said:

It works perfectly.

Thanks....

# January 19, 2010 12:20 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)