As we know GridView is a very popular control. We always experiment to enhance the GridView control to provide better experience to user. Since javascript is also a popular clientside language, most of the times we use this in client side operations like validation, Row coloring, cell coloring etc. Now a days Jquery is more popular so that in this article i will explain how you can capture the selected rows of a GridView control and also read the datakeyname values using JQuery. So at first i will tell you how we capture selected rows of a GridView. In this example i don't consider the built in Select command. Instead of select command here i use checkbox to select rows by user. The another important tip is we cannot directly access DataKeyNames values of a GridView. To get the value, here I will use a template column to store the same DataKeyValue in a hiddenfield. After that through iteration by Jquery I will read the hiddenfield values which looks alike datakeynames values.
For selecting/deselecting all checkboxes of a GridView using Javascript click here.My example output look like below:
The GridView Control HTML will be:
01 | <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"> |
05 | <asp:CheckBox ID="chkSelect" runat="server" /> |
06 | <asp:HiddenField ID="IDVal" runat="server" Value='<%# Eval("ID") %>' /> |
14 | <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label> |
The Jquery Script also given below:
01 | <script type="text/javascript"> |
02 | $(document).ready(function() { |
03 | var gridView1Control = document.getElementById('<%= GridView1.ClientID %>'); |
04 | $('#<%= cmdGetData.ClientID %>').click(function (e) { |
06 | $('input:checkbox[id$=chkSelect]:checked', gridView1Control).each(function (item, index) { |
07 | if(DataKeyName.length==0) |
09 | DataKeyName = $(this).next('input:hidden[id$=IDVal]').val(); |
13 | DataKeyName += "," + $(this).next('input:hidden[id$=IDVal]').val(); |
Now Bind the GridView data within Page_Load Event:
01 | protected void Page_Load(object sender, EventArgs e) |
03 | DataTable dt = new DataTable(); |
06 | dt.Columns.Add("Name"); |
08 | DataRow oItem = dt.NewRow(); |
10 | oItem[1] = "Shawpnendu Bikash Maloroy"; |
15 | oItem[1] = "Bimalendu Bikash Maloroy"; |
20 | oItem[1] = "Purnendu Bikash Maloroy"; |
23 | GridView1.DataSource = dt; |
The complete markup language of this example is:
01 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_DataKeyNames_Jquery.aspx.cs" Inherits="GridView_DataKeyNames_Jquery" %> |
07 | <title>Get Selected rows of a GridView using Jquery</title> |
08 | <script src="Script/jquery.js" type="text/javascript"></script> |
10 | <script type="text/javascript"> |
11 | $(document).ready(function() { |
12 | var gridView1Control = document.getElementById('<%= GridView1.ClientID %>'); |
13 | $('#<%= cmdGetData.ClientID %>').click(function (e) { |
15 | $('input:checkbox[id$=chkSelect]:checked', gridView1Control).each(function (item, index) { |
16 | if(DataKeyName.length==0) |
18 | DataKeyName = $(this).next('input:hidden[id$=IDVal]').val(); |
22 | DataKeyName += "," + $(this).next('input:hidden[id$=IDVal]').val(); |
33 | <form id="form1" runat="server"> |
35 | <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID"> |
39 | <asp:CheckBox ID="chkSelect" runat="server" /> |
40 | <asp:HiddenField ID="IDVal" runat="server" Value='<%# Eval("ID") %>' /> |
48 | <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label> |
56 | <asp:Button ID="cmdGetData" runat="server" Text="Get Data" /> |
Hope you got my trick & now you can read get selected rows of a GrodView control using jQuery
No comments:
Post a Comment