Objective:-Write a Programme In ASP.NET to Check One or More then One GridView Rows using jQuery.
STEPS :-
STEP:- 1 CREATE NEW PROJECT.
Go to File -> New -> Project -> Select asp.net web forms application -> Entry Application Name -> Click OK.
STEP:-2 Attach "jquery-1.7.1.min" file in Scripts Folder
********HTML Code***************
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Check Uncheck all Checkbox in GridView</title>
- <style type="text/css">
- body
- {
- font-family: Arial;
- font-size : 10pt;
- }
- .links
- {
- font-weight: bold;
- }
- </style>
- <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
- <script type="text/javascript" language="javascript">
- $(document).ready(function()
- {
- $('#<%=gdRows.ClientID %>').find('input:checkbox[id$="chkParent"]').click(function() {
- var isChecked = $(this).prop("checked");
- $("#<%=gdRows.ClientID %> [id*=chkSelect]:checkbox").prop('checked',isChecked);
- });
- $('#<%=gdRows.ClientID %>').find('input:checkbox[id$="chkSelect"]').click(function() {
- var flag = true;
- $("#<%=gdRows.ClientID %> [id*=chkSelect]:checkbox").each(function() {
- if($(this).prop("checked") == false)
- flag = false;
- });
- $("#<%=gdRows.ClientID %> [id*=chkParent]:checkbox").prop('checked',flag);
- });
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <br />
- <br />
- <asp:GridView ID="gdRows" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
- BorderWidth="1px" CellPadding="6" Font-Names="Arial" Font-Size="Small" GridLines="None"
- AutoGenerateColumns="False" ForeColor="Black" Width="36%">
- <FooterStyle BackColor="Tan" />
- <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
- <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
- <HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" />
- <AlternatingRowStyle BackColor="PaleGoldenrod" />
- <RowStyle HorizontalAlign="Left" />
- <Columns>
- <asp:TemplateField HeaderText=" ">
- <HeaderTemplate>
- <asp:CheckBox ID="chkParent" runat="server" />
- </HeaderTemplate>
- <ItemTemplate>
- <asp:CheckBox ID="chkSelect" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField ShowHeader="true" DataField="ID" HeaderText="ID"></asp:BoundField>
- <asp:BoundField ShowHeader="true" DataField="ProductName" HeaderText="Product Name">
- </asp:BoundField>
- <asp:BoundField ShowHeader="true" DataField="Quantity" HeaderText="Quantity"></asp:BoundField>
- <asp:BoundField ShowHeader="true" DataField="Price" HeaderText="Price"></asp:BoundField>
- </Columns>
- </asp:GridView>
- </form>
- </body>
- </html>
********C# Code***************
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- BindGrid();
- }
- private void BindGrid()
- {
- DataTable dt = new DataTable();
- DataColumn dc;
- dc = new DataColumn();
- dc.ColumnName = "ID";
- dc.DataType = System.Type.GetType("System.Int32");
- dt.Columns.Add(dc);
- dc = new DataColumn();
- dc.ColumnName = "ProductName";
- dt.Columns.Add(dc);
- dc = new DataColumn();
- dc.ColumnName = "Quantity";
- dc.DataType = System.Type.GetType("System.Int32");
- dt.Columns.Add(dc);
- dc = new DataColumn();
- dc.ColumnName = "Price";
- dc.DataType = System.Type.GetType("System.Int32");
- dt.Columns.Add(dc);
- DataRow dr;
- dr = dt.NewRow();
- dr["ID"] = 1;
- dr["ProductName"] = "Mouse";
- dr["Quantity"] = 10;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 2;
- dr["ProductName"] = "Speaker";
- dr["Quantity"] = 15;
- dr["Price"] = 990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 3;
- dr["ProductName"] = "Hard Drive";
- dr["Quantity"] = 35;
- dr["Price"] = 3990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 4;
- dr["ProductName"] = "RAM";
- dr["Quantity"] = 22;
- dr["Price"] = 399;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 5;
- dr["ProductName"] = "Wireless Keyboard";
- dr["Quantity"] = 10;
- dr["Price"] = 3500;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 6;
- dr["ProductName"] = "Wi-Fi Router";
- dr["Quantity"] = 1;
- dr["Price"] = 3990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 7;
- dr["ProductName"] = "LCD";
- dr["Quantity"] = 62;
- dr["Price"] = 3000;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 8;
- dr["ProductName"] = "Intel Processor";
- dr["Quantity"] = 5;
- dr["Price"] = 7000;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 9;
- dr["ProductName"] = "Monitor";
- dr["Quantity"] = 25;
- dr["Price"] = 1990;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 10;
- dr["ProductName"] = "Keyboard";
- dr["Quantity"] = 20;
- dr["Price"] = 350;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["ID"] = 11;
- dr["ProductName"] = "headphones";
- dr["Quantity"] = 12;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
- gdRows.DataSource = dt;
- gdRows.DataBind();
- }
- }

No comments:
Post a Comment