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();
}
|
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. |
|||
|
|||
25 comments :
Very useful blog...Thanks to the Author
thank u so much....this is really useful......
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?
thank u so much....this is really useful......
thank u so much
Thank you Suresh Dasari, this post Helped me a lot :)
how to show selected value in lable using gridview radio button
Thank you Suresh Dasari, this code is Helped me a lot. Thank you very much.
Thanks ... it is a very important piece of code
Arunendra
it's not working in radiobuttonlist
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
Thanks..
thank u
thanks
in really helpful.perfect working
Really this is very good code
It is good..
Could you tell
how to get the row details of datagridview on selection of radio button.
Thank you
Thank you, it works.
Great example....Thanks...!!!
--Penchal
very useful information!!!!! valuable posts from you.
Thanks alot
thanks it work..but now want to retrieve selected value on button click
Really very helpful saved my day thanks a ton
TY
Note: Only a member of this blog may post a comment.