Sunday, 4 May 2014

Use of Search Data from GridView In ASP.NET.



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***************
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title>Search GridView Data</title>
  5.     <style type="text/css">
  6.     body
  7.     {
  8.         font-family: Arial;
  9.         font-size : 10pt;
  10.     }
  11.     .links
  12.     {
  13.         font-weight: bold;
  14.     }
  15.     </style>

  16.     <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>

  17.     <script type="text/javascript" language="javascript">
  18.     $(document).ready(function() {
  19.         $('#<%=lblNoRecords.ClientID%>').css('display','none');
  20.         
  21.         $('#<%=btnSubmit.ClientID%>').click(function(e)
  22.         {
  23.             $('#<%=lblNoRecords.ClientID%>').css('display','none'); // Hide No records to display label.
  24.             $("#<%=gdRows.ClientID%> tr:has(td)").hide(); // Hide all the rows.
  25.             
  26.             var iCounter = 0;
  27.             var sSearchTerm = $('#<%=txtSearch.ClientID%>').val(); //Get the search box value
  28.             
  29.             if(sSearchTerm.length == 0) //if nothing is entered then show all the rows.
  30.             {
  31.               $("#<%=gdRows.ClientID%> tr:has(td)").show(); 
  32.               return false;
  33.             }
  34.             //Iterate through all the td.
  35.             $("#<%=gdRows.ClientID%> tr:has(td)").children().each(function() 
  36.             {
  37.                 var cellText = $(this).text().toLowerCase();
  38.                 if(cellText.indexOf(sSearchTerm.toLowerCase()) >= 0) //Check if data matches
  39.                 {    
  40.                     $(this).parent().show();
  41.                     iCounter++;
  42.                     return true;
  43.                 } 
  44.             });
  45.             if(iCounter == 0)
  46.             {
  47.                 $('#<%=lblNoRecords.ClientID%>').css('display','');
  48.             }
  49.             e.preventDefault();
  50.         })
  51.     })
  52.     </script>

  53. </head>
  54. <body>
  55.     <form id="form1" runat="server">
  56.         <br />
  57.         <br />
  58.         Search Text :
  59.         <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
  60.         &nbsp;
  61.         <asp:Button ID="btnSubmit" runat="server" Text="Search" />
  62.         <br /><br />
  63.         <asp:GridView ID="gdRows" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
  64.             BorderWidth="1px" CellPadding="6" Font-Names="Arial" Font-Size="Small" GridLines="None"
  65.             ForeColor="Black" Width="36%">
  66.             <FooterStyle BackColor="Tan" />
  67.             <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
  68.             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
  69.             <HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Left" />
  70.             <AlternatingRowStyle BackColor="PaleGoldenrod" />
  71.             <RowStyle HorizontalAlign="Left" />
  72.         </asp:GridView>
  73.         <asp:Label ID="lblNoRecords" Text="No records to display" runat="server" ForeColor="red"></asp:Label>
  74.     </form>
  75. </body>
  76. </html>


********C# Code***************
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;

  10. public partial class _Default : System.Web.UI.Page 
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.             BindGrid();
  16.     }

  17.     private void BindGrid()
  18.     {
  19.         DataTable dt = new DataTable();
  20.         DataColumn dc;
  21.         dc = new DataColumn();
  22.         dc.ColumnName = "ID";
  23.         dc.DataType = System.Type.GetType("System.Int32");
  24.         dt.Columns.Add(dc);

  25.         dc = new DataColumn();
  26.         dc.ColumnName = "ProductName";
  27.         dt.Columns.Add(dc);

  28.         dc = new DataColumn();
  29.         dc.ColumnName = "Quantity";
  30.         dc.DataType = System.Type.GetType("System.Int32");
  31.         dt.Columns.Add(dc);

  32.         dc = new DataColumn();
  33.         dc.ColumnName = "Price";
  34.         dc.DataType = System.Type.GetType("System.Int32");
  35.         dt.Columns.Add(dc);


  36.         DataRow dr;
  37.         dr = dt.NewRow();
  38.         dr["ID"] = 1;
  39.         dr["ProductName"] = "Mouse";
  40.         dr["Quantity"] = 10;
  41.         dr["Price"] = 100;
  42.         dt.Rows.Add(dr);

  43.         dr = dt.NewRow();
  44.         dr["ID"] = 2;
  45.         dr["ProductName"] = "Speaker";
  46.         dr["Quantity"] = 15;
  47.         dr["Price"] = 990;
  48.         dt.Rows.Add(dr);

  49.         dr = dt.NewRow();
  50.         dr["ID"] = 3;
  51.         dr["ProductName"] = "Hard Drive";
  52.         dr["Quantity"] = 35;
  53.         dr["Price"] = 3990;
  54.         dt.Rows.Add(dr);

  55.         dr = dt.NewRow();
  56.         dr["ID"] = 4;
  57.         dr["ProductName"] = "RAM";
  58.         dr["Quantity"] = 22;
  59.         dr["Price"] = 399;
  60.         dt.Rows.Add(dr);

  61.         dr = dt.NewRow();
  62.         dr["ID"] = 5;
  63.         dr["ProductName"] = "Wireless Keyboard";
  64.         dr["Quantity"] = 10;
  65.         dr["Price"] = 3500;
  66.         dt.Rows.Add(dr);

  67.         dr = dt.NewRow();
  68.         dr["ID"] = 6;
  69.         dr["ProductName"] = "Wi-Fi Router";
  70.         dr["Quantity"] = 1;
  71.         dr["Price"] = 3990;
  72.         dt.Rows.Add(dr);

  73.         dr = dt.NewRow();
  74.         dr["ID"] = 7;
  75.         dr["ProductName"] = "LCD";
  76.         dr["Quantity"] = 62;
  77.         dr["Price"] = 3000;
  78.         dt.Rows.Add(dr);

  79.         dr = dt.NewRow();
  80.         dr["ID"] = 8;
  81.         dr["ProductName"] = "Intel Processor";
  82.         dr["Quantity"] = 5;
  83.         dr["Price"] = 7000;
  84.         dt.Rows.Add(dr);

  85.         dr = dt.NewRow();
  86.         dr["ID"] = 9;
  87.         dr["ProductName"] = "Monitor";
  88.         dr["Quantity"] = 25;
  89.         dr["Price"] = 1990;
  90.         dt.Rows.Add(dr);

  91.         dr = dt.NewRow();
  92.         dr["ID"] = 10;
  93.         dr["ProductName"] = "Keyboard";
  94.         dr["Quantity"] = 20;
  95.         dr["Price"] = 350;
  96.         dt.Rows.Add(dr);

  97.         dr = dt.NewRow();
  98.         dr["ID"] = 11;
  99.         dr["ProductName"] = "headphones";
  100.         dr["Quantity"] = 12;
  101.         dr["Price"] = 500;
  102.         dt.Rows.Add(dr);

  103.         gdRows.DataSource = dt;
  104.         gdRows.DataBind();

  105.     }
  106. }


No comments:

Post a Comment