Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to make single radio button selection in gridview using javascript

Dec 20, 2010
Introduction

Here I will explain how to make single radio button selection in gridview using javascript.

Description

In my application I have designed gridview with radio button after completion of all the functionality if try to select radio button I am able to select all the radio buttons in gridview that is like this 

My requirement is to make single radio button selection in gridview for that I have written JavaScript function to maintain single radio button selection in gridview. To implement this first design your aspx page like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
</style>
<script language="javascript" type="text/javascript">
function SelectSingleRadiobutton(rdbtnid) {
var rdBtn = document.getElementById(rdbtnid);
var rdBtnList = document.getElementsByTagName("input");
for (i = 0; i < rdBtnList.length; i++) {
if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id)
{
rdBtnList[i].checked = false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" DataKeyNames="UserId" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton id="rdbUser" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="Name"/>
<asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

In code behind bind your gridview and check it 


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
protected void BindGridviewData()
{
SqlConnection con = new SqlConnection("Data Source=MYSystem;Initial Catalog=MySamplesDB;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select UserId,UserName,FirstName,LastName,Location from User_Information", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvdata.DataSource = ds;
gvdata.DataBind();
con.Close();
}

Demo


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

25 comments :

Anonymous said...

Very useful blog...Thanks to the Author

Anonymous said...

thank u so much....this is really useful......

SKKO said...

I also using radio selection on Gridview.
My radio button selection is also working for single selection.
now what I want to know is:
when I selected one radio button,
I want GridView SelectIndex chaging event occurs
like Select Command.
can I use?
if like Select Command, I can easily call like
GridView1.SelectedRow. ..etc..
but now I cannot use like that
I just lopping GridView rows if radio is selected or not.
do u have any refreences?

Rishikesha said...

thank u so much....this is really useful......

Anonymous said...

thank u so much

Anonymous said...

Thank you Suresh Dasari, this post Helped me a lot :)

Unknown said...

how to show selected value in lable using gridview radio button

shekar said...

Thank you Suresh Dasari, this code is Helped me a lot. Thank you very much.

Anonymous said...

Thanks ... it is a very important piece of code

Arunendra

Anonymous said...

it's not working in radiobuttonlist

Unknown said...

thanks for this
but m find out in a grid view two columns use radio button if m apply this code then both two columns buttons are treated as same
bt i want to seprate column and i want to aaply in both column different code

Bharat Gunjal said...

Thanks..

Anonymous said...

thank u

Anonymous said...

thanks

RS Prajapati said...

in really helpful.perfect working

Praveen said...

Really this is very good code

Unknown said...

It is good..
Could you tell
how to get the row details of datagridview on selection of radio button.

Anonymous said...

Thank you

Anonymous said...

Thank you, it works.

Anonymous said...

Great example....Thanks...!!!

--Penchal

Unknown said...

very useful information!!!!! valuable posts from you.

Anonymous said...

Thanks alot

Unknown said...

thanks it work..but now want to retrieve selected value on button click

Nam said...

Really very helpful saved my day thanks a ton

Unknown said...

TY

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.