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

jQuery Get Selected Row Values from Asp.net Gridview on Checkbox Selection

Oct 8, 2012
Introduction

Here I will explain how to get selected row value based on checkbox selection values in gridview using
JQuery in asp.net.

Description:
  
In previous posts I explained jQuery Check uncheck all checkboxes in gridview, jQuery Higlight selected Gridview row when checkbox selected, jQuery select header checkbox when all child checkboxes selected
Best jQuery Drag and Drop Plugins and many articles relating to JQuery. Now I will explain how to get selected row value based on checkbox selection values in gridview using JQuery in asp.net.

To implement this functionality we need to write the code in Default.aspx page like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Get selected row value based on checkbox selection values in gridview using JQuery in asp.net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#btnGet').click(function() {
var hdntxt = '';
$("input[name$=chkChild]:checked").each(function() {
hdntxt += "," + $(this).next("input[name$=hdnId]").val()
});
$('#lbltxt').text(hdntxt.substring(1,hdntxt.length))
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserInfo" runat="server" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkChild" runat="server" />
<asp:HiddenField ID="hdnId" runat="server" Value='<%#Eval("UserName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<input type="button" id="btnGet" value="Get Selected Values" /><br /><br />
<b>Select UserNames:</b><label id="lbltxt"/>
</form>
</body>
</html>
Now add following namespaces in codebehind
C# Code


using System;
using System.Data;
using System.Data.SqlClient;
After that add following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select UserName,LastName,Location from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is used to bind gridview from database
Protected Sub BindGridview()
Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select UserName,LastName,Location from UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
End Class
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

8 comments :

Unknown said...

this is what i was blindly searching..great and nice job..kamaal kidda..sir ji..

Anonymous said...

hello this is pragya

i got the following error :-

Multiple controls with the same ID 'hdnID' were found. FindControl requires that controls have unique IDs.

Anonymous said...

how can i store label value into a string in cs file??

Anonymous said...

please help me sir, actually i want to move the selected row to another database. for this i need the value of label. how to get that value

অবান্তর e-পত্রিকা said...

how to use the value/text of lbltxt?

indrajith said...

hi it is very useful thanks

Anonymous said...

Dear Sir , Please provide source

Anonymous said...

Most cool! Thank you :)

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.