How To Add Search Feature To GridView-ASP.NET

<%@ Page Language="C#" AutoEventWireup="true"  
Inherits="GridViewAndUpdatePanel" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns="">  
<head runat="server">  
<title>Untitled Page</title>  
<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">  
                <asp:GridView ID="gridViewPersons" runat="server"  
                    ForeColor="#333333" GridLines="None" Width="460px">  
                    <FooterStyle BackColor="#507CD1" Font-Bold="True"  
ForeColor="White" />  
                        <asp:BoundField DataField="Surname"  
HeaderText="Surname" InsertVisible="False" ReadOnly="True">  
                            <HeaderStyle HorizontalAlign="Left" />  
                        <asp:BoundField DataField="Name" HeaderText="Name"  
InsertVisible="False" ReadOnly="True">  
                            <HeaderStyle HorizontalAlign="Left" />  
                        <asp:BoundField DataField="Street" HeaderText="Street"  
InsertVisible="False" ReadOnly="True">  
                            <HeaderStyle HorizontalAlign="Left" />  
                    <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:AsyncPostBackTrigger ControlID="lbFilter" />  
        <br />  
        <asp:UpdateProgress ID="udProgress" runat="server"  
DisplayAfter="100" Visible="true"  
             <div style="color:Red">Loading.....</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"  
        <asp:LinkButton ID="lbFilter" runat="server"  
OnClick="lbFilter_Click">Apply filter</asp:LinkButton></div>  
    <br />  
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)  


private void FillGridView(string filter)  
    List<Person> persons = Helper.GeneratePersons(filter);  
    gridViewPersons.DataSource = persons;  

protected void lbFilter_Click(object sender, EventArgs e)  

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))  


    return filteredPersons;  

Next Post Previous Post
No Comment
Add Comment
comment url