How to get GridView cell value using jQuery in ASP.NET
by GetCodeSnippet.com • June 19, 2013 • jQuery and Microsoft .NET, Microsoft .NET C# (C-Sharp), Microsoft ASP.NET, Microsoft VB.NET • 0 Comments
Getting GridView cell values using jQuery in ASP.NET is so simple. Let me show you how easily we can achieve it. Download any jQuery stable version and add to your project.
- Don’t forget to give reference to you jQuery file.
1 |
<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script> |
- Add a GridView, button and a label.
1 2 3 4 5 |
<asp:GridView ID="gvNames" runat="server"> </asp:GridView> <input id="btnGetNames" type="button" value="Get Names" /> <br /> <asp:Label ID="lblMessage" runat="server"></asp:Label> |
- Bind your GridView with data
VB.NET
1 2 3 4 5 6 7 8 |
Dim myConn As String = "Your connection string here" Dim conObj As New SqlConnection(myConn) Dim comObj As New SqlCommand("Your query here", conObj) Dim dtObj As New DataTable() conObj.Close() gvNames.DataSource = dtObj gvNames.DataBind() |
C#
1 2 3 4 5 6 7 8 |
string myConn = "Your connection string here"; SqlConnection conObj = new SqlConnection(myConn); SqlCommand comObj = new SqlCommand("Your query here", conObj); DataTable dtObj = new DataTable(); conObj.Close(); gvNames.DataSource = dtObj; gvNames.DataBind(); |
- Now right below code to get GridView cell values using jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<script type="text/javascript"> $(document).ready(function () { var d = ""; $("#btnGetNames").click(function () { $("#<%=gvNames.ClientID %> tr").each(function () { if (!this.rowIndex) return; var name = $(this).find("td:last").html(); d += name + "<br/>"; }); $("#lblMessage").append(d) }); }); </script> |
Download Sample Project806 downloads