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.
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 Email
                                      
 | 
|||

                                        Subscribe by RSS
                                      
                                        Subscribe by Email
                                      
8 comments :
this is what i was blindly searching..great and nice job..kamaal kidda..sir ji..
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.
how can i store label value into a string in cs file??
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
how to use the value/text of lbltxt?
hi it is very useful thanks
Dear Sir , Please provide source
Most cool! Thank you :)
Note: Only a member of this blog may post a comment.