| Introduction Hi, in this article I am going to explain you about the use of “radio button” control inside an “ASP.NET Grid View” control. This sounds that it’s easy to put the radio button inside a grid view control, but you are wrong it’s not easy as it sounds. There are many factors which makes this problem complex to solve. But as you know the things looks “impossible”, who ultimately says “I am possible”. So let’s take a look of this problem and solution for the same. |
What is the problem? The problem is, sometimes we (web developers) needs to display a data to the user which requires a radio button control, means for selection purpose or something. Let’s say you have a list of products out of which you want user can select only one product, so radio button help us that user can select only one product. List something like below: |
Description of the problem and its solution So in the above displayed case we need to put radio button control inside a column in the grid view. Now as you know in asp.net we use the grid view control to display the data in the tabular format, grid view is an element in which you just need to specify the field name (fields that are present in your dataset) which you want to display. And then it will automatically bind those fields and displays all the data to the user in a tabular format. But according to our problem we need to have a radio button control inside a column in the grid view. In the grid view there are 7 predefined column types using those you can bound text data and controls like button, check box etc in the grid view column. The 7 type of columns are displayed below. |
|
There are 2-3 fields present which are meant and developed to bind the controls like checkbox, hyperlink, image, and button. But not for radio button, so if you want to display any other control in the grid view columns other than already there then you have to use the Template Field column. In which you can place any control you want. According to our problem we have to display a radio button inside the grid column then we’ll take the Template field column to get the radio button displayed inside it. |
Types of “Radio Button” in ASP.NET Now the complex things start. I don’t know you people are aware of it or not, but there are two types of radio buttons present in the asp.net |
|
| Now the question is which type of radio button to choose to get our problem solved. If we choose the “Server radio button control” then it will create problem because the “groupName” property (which is responsible for single radio button selection among the many radio buttons present in the grid) will work only for the radio button present in the same row, not in the different rows, so this will not work and solve our problem. Now the other one “HTML radio button control” which solve our problem because it works for the “name” (which is responsible for single selection) different rows, and also for the same row. That means where ever the HTML radio control will find the radio control which is having “name” property as same it will include that radio control in the group also and only single selection is possible. So the final result of this section is that we’ll go with “HTML Radio Button Control” and we’ll use this in our template field column inside a grid view. |
Solution code for the problem I have created an example in Asp.NET (C#) to fill the grid view with a radio button column. Like below: To demonstrate this problem I have created a table in database, an asp.net application, which I am explaining here and later you can download the full source of it. SQL Table Script: CREATE TABLE radioproblem INSERT INTO radioproblem VALUES(’Prashant’) SELECT * FROM radioproblem ASP.NET Application <asp:TemplateField> The markup-code of grid view that you will find inside the example, only the above code deserves an explanation. In the above code we have taken a template field column, in which we have inserted a HTML radio button control, <input type=”radio” name=”gvradio” value=”<%#Eval(”Name”)%>” /> In the radio button code you have to set the “name” attribute to something which you want and “value” this is basically our database value which you can set using the Eval method and we’ll retrieve when user submit the page. Name property is must because this property insures that only single radio button will be clicked out of all radio buttons having the same name, and also we’ll be able to get the value of this radio button using this name. Hold on guys, |
Getting value of “HTML Radio Button” control present inside grid view Now our first problem after solving the above problem is “getting the value of selected radio button in asp.net code”. It looks easy but its not, because the radio button is present inside the grid view, so you can’t directly access this control and also it’s a HTML radio button, So according to ASP.NET we can’t access the HTML controls directly in Server side code. So how will we find out that, which product or record user selected? So there is a way with which we can get the value that which record is selected. Generally you can bind any value with radio button which you ant to access on the post back, but as we know In every sql database table (mostly) there is a primary key column which identifies each row uniquely; it may be your product id, employee id, recorded etc. So it’s always better to bind that unique field with radio button type of columns so that you can find out each row in grid uniquely, but all depends on your requirement whatever you want bind with radio button control you can bin, like I am binding my table’s “Name” field with radio button in my example so that I can access that which name is selected when user submit the page. Have a look how to do this as I have done below: <input type=”radio” name=”gvradio” value=”<%#Eval(”Name”)%>” /> Request.Form["gvradio"]; Using the above code you can get the value selected on click of the submit button. In my example I am do is,on click of submit button I am changing the value of Label1 label present on my web page. Like below: Label1.Text = Request.Form["gvradio"]; So now you got he value of the radio button in asp.net code also. But there is one more possible problem that can arise problems for you, but d’t worry that also having the solution, lets take a look. |
Selecting the first radio button by default Another problem is “selecting the first radio button in the grid view by default”, if you’ll not do so then, without selecting any record user will submit the page and hence you have to put a code on server side that user selected any record or not. But for this small thing if we send the user to the server then it will put some amount of load on our server, So to solve this there are two ways out, first is using the checked property (but its not the good way) and second is using JavaScript code. So the first method is using the checked attribute of the radio button you can set the radio button state as checked. Like below, <input type=”radio” name=”gvradio” checked=”checked” value=”<%#Eval(”Name”)%>” /> <script language=”javascript” type=”text/javascript”> while(chkChecked) In above code I am taking the reference of gridview as a table object then after accessing the 1st cell of 1st row and then searching the INPUT tag inside that cell if found then checking that it is a radio button, if yes then selecting the radio button and stooping the loop. <script type=”text/javascript” language=”javascript”>select_first();</script> put the above code after the grid tag (</asp:GridView>) close. So that it will run after the grid completely rendered and hence it will select the radio button. |
| Download the example code Download the zip file and change the SQL database name, SQL Server username and password, and also make a table using the script given in the example, because that table is used to demonstrate the application. |
|
TI have tried my best to explain all possible problem of gridview with radio button, else you can download and run the sample application. To run the code you have to create the table in SQL and have to give the SQL username and password in the code file. If feel any problem in understanding the code then please comment here.
|
| Reference : Prashant Pandey (TechAhead) |
|
******* |
ASP.NET – Using "radio button" inside grid view
April 1, 2008 by Prashant Pandey

Very good…. tnks….
Actually this example is very helpful but i want to know about checkboxes in place of radio buttons for multiple selections and got the data of selected row to insert in database
Hi,
if you want to multiple selection using checkbox, use this link
http://social.msdn.microsoft.com/Forums/en-US/vswebdeveloperexpresspt/thread/2e787edd-aba7-452c-9eb3-3694872b8f9a
Im facing problem of using radioButtonList inside the gridview
rathar than using single radio iwant to use list of radio button inside gridview,pls help me and provide the solution
Hi,
I too had the same problem but his code helped me greatly, but still i have problem, once i select the particular row and post back that page again when i come back i need to maintains the state of the selected radio button.. how can that be achieved in this control.
Same issue as Santosh, trying to maintain state after a submittal, the selected radio button is cleared??
thanks,
Kevin
Happy.. thnks
That was great..Thank you very much
Hi Prashant ,
It’s a very good article.I am new to asp.net, i am getting a error “Login failed for user ‘Ram’. The user is not associated with a trusted SQL Server connection”.i am running this application on my PC
Ram is my windows Login id,$@iram80 is password.The connection string is as follows
SqlConnection con = new SqlConnection(”Data Source=localhost;uid=Ram;password=$@iram80;Integrated Security=False;Persist Security Info=False;”);
Looking forward to hear from u
Regards
SriRam
thx 4 tips
i need athe code to redirect a .aspx web page when i click the radio button in my .aspx page
i need the code to redirect a .asp.net web page when i click the radio button in another asp.net page
It’s a very VERY good article. Solved all my problem regarding radiobuttons in GridView
thanks
Kristinn
great work!
hi
thanks a lot for the rdobutton code its very simple. it saved lots of my efforts.
but facng the selction problem even after using java script as written.
the cursors is dispalyed in the last record only. how doi display it in the first record.
i have called the java script after
Thanks Dear
You are a good person and programmer too. It will help me a lot.
RAJIV ROHILLA
SAMALKHA Haryana (London UK)
good…
Hi
Here i got the best solution for my problem and the way these things are explained is soooooo good.
Thanks,
Wohooooo!!!! your a genuis not kiddin. Im still a student and from the code you posted.. Wohooooo! Im like lookin to a code very imposible for me to create… Master or should I say Sensie!!! will Ya give me sum tips or other good stuffs for me to improve my programming skill…. Heres my “email den010@yahoo.com“…….Thanks a lot!!!!
Ha-ha-ha. This`s a bad sample – u can`t select a radiobutton instead the first one!
Hi,
Thanks Prashant.
But it showing “Microsoft JScript runtime error: Object required” when tried to run the sample code.
The problem is
select_first();
put the above code after the grid tag () close.
The script method is called before the Gridview is rendered.
i.e before clicking Fill Data button .
gr8..its working…..
Nice code Thanks
How to get if there are 3 radio buttons like (Yes,NO,Maybe).
We have to get which radio button is selected i.e…. either Yes or No nor Maybe
Please help abou that
thanks!
that solved my problem!
Hi..
Cud u help me wit the usage of radiobuttonlist in gridview in a similar elaborative manner?
But why you want to use a radiobuttonlist inside a gridview? normally in a single row you need only one radiobutton, which selects that particular row, and that is demonstrated in the sample.
Anyways, try this link: http://www.daniweb.com/forums/thread151729.html#
May be it will help you…
Thanks for the reply.
Im supposed to select from a list of choices in each row.
For Ex: A Questionaire which has a list of questions and corresponding choices for each question..The choices are to be represented through radiobuttonlist while the questions in a gridview.
I ve alredy done the above mentioned but after selection of a choice and on clicking the submit button the database is not getting updated.
Awesome,Mind Blowing, i never saw such an explanation in this outstanding way. This is my ever first feedback to any site. Ur explanation way is really superb and it help me really so much.
Good sample code