Searching in GridView Example

Code file
--------
using System;using System.Data;using System.Configuration;using System.Collections;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;using System.Data.SqlClient;using
System.Text.RegularExpressions;public partial class Default2 : System.Web.UI.Page{


{

{
BindGrid();
}
}

{

conn.Open();

cmd.Connection = conn;
cmd.CommandType =
cmd.CommandText =

dAdapter.SelectCommand = cmd;

dAdapter.Fill(objDs);

}

{


{
grdSearch.DataSource = dt;
grdSearch.DataBind();
}
}

{




{
SearchExpression =
grdSearch.SortExpression, txtSearch.Text);
}
dv.RowFilter =
grdSearch.DataSource = dv;
grdSearch.DataBind();
}

{

string strConnection = @"Data Source=JSP1;Initial Catalog=db_test;User ID=sa; Password=comsa";protected void Page_Load(object sender, EventArgs e)if (!IsPostBack)private DataTable GetRecords()SqlConnection conn = new SqlConnection(strConnection);SqlCommand cmd = new SqlCommand();CommandType.Text;"Select * from Employee";SqlDataAdapter dAdapter = new SqlDataAdapter();DataSet objDs = new DataSet();return objDs.Tables[0];private void BindGrid()DataTable dt = GetRecords();if (dt.Rows.Count > 0)private void SearchText()DataTable dt = GetRecords();DataView dv = new DataView(dt);string SearchExpression = null;if (!String.IsNullOrEmpty(txtSearch.Text))string.Format("{0} '%{1}%'","Emp_Name like" + SearchExpression;public string Highlight(string InputTxt)string Search_Str = txtSearch.Text.ToString();// Setup the regular expression and add the Or operator.

Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),RegexOptions.IgnoreCase);// Highlight keywords by calling the //delegate each time a keyword is found.

return RegExp.Replace(InputTxt,new MatchEvaluator(ReplaceKeyWords));// Set the RegExp to null.RegExp =
}

{

}

{
SearchText();
}
}

Source File
-------------
<%
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"Namespace="System.Web.UI" TagPrefix="asp" %><!
<
<
<
<
<
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">link href ="App_Themes/Theme1/StyleSheet.css" rel ="Stylesheet" type ="text/css" />title>Grid Search example</title>style type="text/css">.highlight {text-decoration:none; font-weight:bold;color:black; background:yellow;}</
</
<
<
style>head>body>form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1" runat="server">
<
</asp:ScriptManager>div>Enter employee name to search:</
<
<
div>div>asp:TextBox ID="txtSearch" runat="server"AutoPostBack="True"OnTextChanged
</
="txtSearch_TextChanged">asp:TextBox>&nbsp;</
<
<
div>div>asp:UpdatePanel ID="UpdatePanel1"runat
<
<
="server">ContentTemplate>asp:GridView ID="grdSearch"runat="server"AutoGenerateColumns
<
<
<
<
="false">Columns>asp:TemplateField HeaderText="Emp_Name">ItemTemplate>asp:Label ID="lblFirstName"runat="server"Text
</
</
</
<
<
<
='<%# Highlight(Eval("Emp_Name").ToString()) %>'>asp:Label>ItemTemplate>asp:TemplateField>asp:TemplateField HeaderText="Emp_Dept">ItemTemplate>asp:Label ID="lblLastName" runat="server"Text
</
</
<
<
<
='<%#(Eval("Emp_Dept")) %>'></asp:Label>ItemTemplate>asp:TemplateField>asp:TemplateField HeaderText="Emp_Salary">ItemTemplate>asp:Label ID="lblLocation" runat="server"Text
</
</
</
</
</
<
<
='<%#(Eval("Emp_Salary")) %>'></asp:Label>ItemTemplate>asp:TemplateField>Columns>asp:GridView>ContentTemplate>Triggers>asp:AsyncPostBackTrigger ControlID="txtSearch"EventName
</
</
</
</
</
</
="TextChanged" />Triggers>asp:UpdatePanel>div>form>body>html>
null;public string ReplaceKeyWords(Match m)return "<span class=highlight>" + m.Value + "</span>";protected void txtSearch_TextChanged(object sender, EventArgs e)

0 comments:

Post a Comment