Thursday, 8 August 2013

ASPDOTNET SEARCH RECORDS IN GRIDVIEW AJAX MANOHAR GOUD

9866550012
ASPDOTNET




Add following CSS style in head section of page.
1<style type="text/css">
2.highlight
3{
4text-decoration:none; font-weight:bold;
5color:black; background:yellow;
6}
7</style>


HTML SOURCE

<asp:ScriptManager ID="ScriptManager1" runat="server"/> Enter first name to search:

<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" OnTextChanged="txtSearch_TextChanged"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate><asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="false"><Columns><asp:TemplateField HeaderText="FirstName"><ItemTemplate><asp:Label ID="lblFirstName" runat="server" Text='<%# Highlight(Eval("FirstName").ToString()) %>'/></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="LastName"><ItemTemplate><asp:Label ID="lblLastName" runat="server" Text='<%#(Eval("LastName")) %>'/></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Location"><ItemTemplate><asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("Location")) %>'/></ItemTemplate></asp:TemplateField></Columns></asp:GridView></ContentTemplate><Triggers><asp:AsyncPostBackTrigger ControlID="txtSearch" EventName="TextChanged" /></Triggers></asp:UpdatePanel>

C# CODE
protected void Page_Load(object sender, EventArgs e)
02    {
03        if (!IsPostBack)
04        {
05            BindGrid();
06        }
07    }
08    private DataTable GetRecords()
09    {
10        SqlConnection conn = new SqlConnection(strConnection);
11        conn.Open();
12        SqlCommand cmd = new SqlCommand();
13        cmd.Connection = conn;
14        cmd.CommandType = CommandType.Text;
15        cmd.CommandText = "Select * from Employees";
16        SqlDataAdapter dAdapter = new SqlDataAdapter();
17        dAdapter.SelectCommand = cmd;
18        DataSet objDs = new DataSet();
19        dAdapter.Fill(objDs);
20        return objDs.Tables[0];
21 
22    }
23    private void BindGrid()
24    {
25        DataTable dt = GetRecords();
26        if (dt.Rows.Count > 0)
27        {
28            grdSearch.DataSource = dt;
29            grdSearch.DataBind();
30        }
31    }
32    private void SearchText()
33    {
34        DataTable dt = GetRecords();
35        DataView dv = new DataView(dt);
36        string SearchExpression = null;
37        if (!String.IsNullOrEmpty(txtSearch.Text))
38        {
39            SearchExpression = string.Format("{0} '%{1}%'",
40            grdSearch.SortExpression, txtSearch.Text);
41 
42        }
43        dv.RowFilter = "FirstName like" + SearchExpression;
44        grdSearch.DataSource = dv;
45        grdSearch.DataBind();
46 
47    }
48    public string Highlight(string InputTxt)
49    {
50        string Search_Str = txtSearch.Text.ToString();
51        // Setup the regular expression and add the Or operator.
52        Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
53        RegexOptions.IgnoreCase);
54 
55        // Highlight keywords by calling the
56        //delegate each time a keyword is found.
57        return RegExp.Replace(InputTxt,
58        new MatchEvaluator(ReplaceKeyWords));
59 
60        // Set the RegExp to null.
61        RegExp = null;
62 
63    }
64 
65    public string ReplaceKeyWords(Match m)
66    {
67 
68        return "<span class="highlight">" + m.Value + "</span>";
69 
70    }
71 
72    protected void txtSearch_TextChanged(object sender, EventArgs e)
73    {
74        SearchText();
75    }



No comments:

Post a Comment