How To Add Search Feature To GridView-ASP.NET

<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="GridViewAndUpdatePanel.aspx.cs"  
Inherits="GridViewAndUpdatePanel" %>  
 
<!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>Untitled Page</title>  
</head>  
<body>  
<form id="form1" runat="server">  
    <!-- You need to add this ScriptManager -->  
    <asp:ScriptManager ID="scriptMgr" runat="server" />  
    <div style="height: 190px">  
        <!-- Only this area is updated on PostBack -->  
        <asp:UpdatePanel ID="updatePnl" runat="server" UpdateMode="Conditional">  
            <ContentTemplate>  
                <asp:GridView ID="gridViewPersons" runat="server"  
AutoGenerateColumns="False"  
CellPadding="4"  
                    ForeColor="#333333" GridLines="None" Width="460px">  
                    <FooterStyle BackColor="#507CD1" Font-Bold="True"  
ForeColor="White" />  
                    <Columns>  
                        <asp:BoundField DataField="Surname"  
HeaderText="Surname" InsertVisible="False" ReadOnly="True">  
                            <HeaderStyle HorizontalAlign="Left" />  
                        </asp:BoundField>  
                        <asp:BoundField DataField="Name" HeaderText="Name"  
InsertVisible="False" ReadOnly="True">  
                            <HeaderStyle HorizontalAlign="Left" />  
                        </asp:BoundField>  
                        <asp:BoundField DataField="Street" HeaderText="Street"  
InsertVisible="False" ReadOnly="True">  
                            <HeaderStyle HorizontalAlign="Left" />  
                        </asp:BoundField>  
                    </Columns>  
                    <RowStyle BackColor="#EFF3FB" />  
                    <EditRowStyle BackColor="#2461BF" />  
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"  
ForeColor="#333333" />  
                    <PagerStyle BackColor="#2461BF" ForeColor="White"  
HorizontalAlign="Center" />  
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True"  
ForeColor="White" />  
                    <AlternatingRowStyle BackColor="White" />  
                </asp:GridView>  
            </ContentTemplate>  
            <Triggers>  
                <asp:AsyncPostBackTrigger ControlID="lbFilter" />  
            </Triggers>  
        </asp:UpdatePanel>  
        <br />  
        <asp:UpdateProgress ID="udProgress" runat="server"  
DisplayAfter="100" Visible="true"  
            DynamicLayout="true">  
            <ProgressTemplate>  
             <div style="color:Red">Loading.....</div>  
            </ProgressTemplate>  
        </asp:UpdateProgress>  
    </div>  
    <!-- Update is initiated here, outside the UpdatePanel -->  
    <div style="background: url('img/background.png') repeat-x;  
width: 450px; height: 25px;  
        border: solid 1px #cccccc; padding: 5px; color: #666666">  
        Filter Name (first letters):  
        <asp:TextBox CssClass="MainFont" ID="txtName"  
runat="server"></asp:TextBox>  
        <asp:LinkButton ID="lbFilter" runat="server"  
OnClick="lbFilter_Click">Apply filter</asp:LinkButton></div>  
    <br />  
</form>  
</body>  
</html>  
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.Collections.Generic;  

public partial class GridViewAndUpdatePanel : System.Web.UI.Page  
{  
protected void Page_Load(object sender, EventArgs e)  
{  
    FillGridView("");  


}  

private void FillGridView(string filter)  
{  
    System.Threading.Thread.Sleep(1000);  
    List<Person> persons = Helper.GeneratePersons(filter);  
    gridViewPersons.DataSource = persons;  
    gridViewPersons.DataBind();  
}  




protected void lbFilter_Click(object sender, EventArgs e)  
{  
    FillGridView(txtName.Text);  
}  
}  

public class Person  
{  
public Person(string surname, string name, string street)  
{  
    _street = street;  
    _name = name;  
    _surname = surname;  
}  

private string _street;  
public string Street  
{  
    get { return _street; }  
    set { _street = value; }  
}  

private string _name;  
public string Name  
{  
    get { return _name; }  
    set { _name = value; }  
}  

private string _surname;  
public string SurName  
{  
    get { return _surname; }  
    set { _surname = value; }  
}  
}  
public class Helper  
{  
public static List<Person> GeneratePersons(string filter)  
{  
    List<Person> persons = new List<Person>();  

    persons.Add(new Person("Hunter S.", "Thompson", "Bakerstreet"));  
    persons.Add(new Person("Chris", "Byron", "Williamsstreet"));  
    persons.Add(new Person("Paul", "Stanley", "Hunterstreet"));  
    persons.Add(new Person("Charly", "Bukowski", "Mainstreet"));  
    persons.Add(new Person("Will", "Burroughs", "Lurkerstreet"));  

    if (filter.Trim() == "")  
        return persons;  

    List<Person> filteredPersons = new List<Person>();  
    foreach (Person person in persons)  
    {  
        if (person.Name.StartsWith(filter))  
            filteredPersons.Add(person);  

    }  

    return filteredPersons;  



}  
}

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم