Objective:-Use of Search Data from GridView In ASP.NET.
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 runat="server">
- <title>Search GridView Data</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() {
- $('#<%=lblNoRecords.ClientID%>').css('display','none');
- $('#<%=btnSubmit.ClientID%>').click(function(e)
- {
- $('#<%=lblNoRecords.ClientID%>').css('display','none'); // Hide No records to display label.
- $("#<%=gdRows.ClientID%> tr:has(td)").hide(); // Hide all the rows.
- var iCounter = 0;
- var sSearchTerm = $('#<%=txtSearch.ClientID%>').val(); //Get the search box value
- if(sSearchTerm.length == 0) //if nothing is entered then show all the rows.
- {
- $("#<%=gdRows.ClientID%> tr:has(td)").show();
- return false;
- }
- //Iterate through all the td.
- $("#<%=gdRows.ClientID%> tr:has(td)").children().each(function()
- {
- var cellText = $(this).text().toLowerCase();
- if(cellText.indexOf(sSearchTerm.toLowerCase()) >= 0) //Check if data matches
- {
- $(this).parent().show();
- iCounter++;
- return true;
- }
- });
- if(iCounter == 0)
- {
- $('#<%=lblNoRecords.ClientID%>').css('display','');
- }
- e.preventDefault();
- })
- })
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <br />
- <br />
- Search Text :
- <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
-
- <asp:Button ID="btnSubmit" runat="server" Text="Search" />
- <br /><br />
- <asp:GridView ID="gdRows" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
- BorderWidth="1px" CellPadding="6" Font-Names="Arial" Font-Size="Small" GridLines="None"
- 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" />
- </asp:GridView>
- <asp:Label ID="lblNoRecords" Text="No records to display" runat="server" ForeColor="red"></asp:Label>
- </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