SIGN UP MEMBER LOGIN:    
ARTICLE

Select Single Radio Button in Gridview in Asp.Net

Posted by Krishna Garad Articles | ASP.NET Programming March 16, 2011
In this article we will learn how to select only one row of a Gridview using RadioButtons whereas normally a RadioButton does not support selection of only one RadioButton within a row.
Reader Level:
Download Files:
 


Introduction:

In this article we will learn how to select only one row of a GridView using RadioButtons whereas normally a RadioButton does not support selection of only one RadioButton within a row.

Background:

In ASP.Net the GridView control does not support selection of a single RadioButton that works as a group across rows. Generally as you know a RadioButton is a control that works in a group for selecting only one option. If we take RadioButtonList control it will work everywhere, which applies to a GridView also but if we use only one RadioButton control for each row in GridView it will not allow us to select only one. Try selecting a RadioButton in every row; it will allow selection of multiple rows in GridView. Here we will see how to restrict the user to select only one RadioButton within a GridView control.

For doing this task I've written some Javascript function. This function actually I got from somewhere while Google search. We will step by step follow.

Step 1:

Start new website. Put the GridView control on the Default.aspx page with RadioButton control in TemplateField like bellow.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="AuthId">
            <Columns>
            <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
            <asp:RadioButton ID="rdbauthid" runat="server" onclick="javascript:CheckOtherIsCheckedByGVID(this);" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="AUTHOR NAME" DataField="AuthName" />
            <asp:BoundField HeaderText="AUTHOR LOCATION" DataField="AuthLocation" />
            </Columns>
        </asp:GridView>

In the above you may see that in the Click event of the RadioButton control I call a Javascript function.

Step 2:

Write the script as below for checking the current selection of the RadioButton control; if more than one is selected then the other selection is removed. Other than this there are two more methods to do same task.

<script type="text/javascript">
            function CheckOtherIsCheckedByGVID(spanChk) {

                var IsChecked = spanChk.checked;

                if (IsChecked) {

                    spanChk.parentElement.parentElement.style.backgroundColor = '#228b22';

                    spanChk.parentElement.parentElement.style.color = 'white';

                }

                var CurrentRdbID = spanChk.id;

                var Chk = spanChk;

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

                var items = Parent.getElementsByTagName('input');

                for (i = 0; i < items.length; i++) {

                    if (items[i].id != CurrentRdbID && items[i].type == "radio") {

                        if (items[i].checked) {

                            items[i].checked = false;

                            items[i].parentElement.parentElement.style.backgroundColor = 'white'
 
                            items[i].parentElement.parentElement.style.color = 'black';
                        }

                    }

                }

            }
</script>


Step 3:

Bind your GridView in aspx.cs page and run the application.

Conclusion:

In this way we can restrict the user to select only one option via RadioButton in GridView.
 

Login to add your contents and source code to this article
share this article :
post comment
 

this article is really helpfull to me Thanks........ http://trickgeek.blogspot.com/

Posted by milan patel Nov 24, 2011

Thank you very much it will help me much more

Posted by Mayur Gujrathi May 13, 2011

we are creating this event for any control. you can write onclick for any server side control.

Posted by Krishna Garad May 13, 2011

Thanks I got it now But again i ahve arise with one question if this event is not available for radiobuttons then how it runs fine after after writting onclick for radiobutton

Posted by Mayur Gujrathi May 13, 2011

It's available only for those control which have a click event. Check it same for CheckBox dosen't contain onclick event only Button,ImageButton those control have the onclick event.

Posted by Krishna Garad May 13, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor