Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hello all
Scenario:
i wanna implement a filtering feature in my gridviews using entity framework.
Approach:
I added a text box to the header template of the gridview and filter the data based on the text entered the textbox by adding the filtering approach inside the TextChanged event of the textbox

Test conducted:
before things get complicated, I wanted to test my logic so what i did is on the form load i selected all the data from a table and in the text changed event i just selected the top 5 rows so that the change is obvious.

Test results:
when I debugged my code i found out that the top 5 rows are being assigned to the gridview datasource but that doesnt reflect on the page

Markup code:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Controls_test.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label4" runat="server" Text="" ></asp:Label>
    <div>



    </div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" />

            </Triggers>
            <ContentTemplate>
                <asp:Label ID="Label3" runat="server" Text="" ViewStateMode="Enabled" ></asp:Label>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:TemplateField HeaderText="Name">
                            <HeaderTemplate>
                                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
                <br />
                                <asp:TextBox ID="txtfilterName" OnTextChanged="txtfilterName_TextChanged" runat="server" AutoPostBack="True"></asp:TextBox>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%#Eval("Title") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
            </ContentTemplate>

        </asp:UpdatePanel>
    </form>
</body>
</html>



Backend code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Controls_test
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


                using (var dc = new SPISDBOnlineEntities())
                {
                    GridView1.DataSource = dc.ViewProjects.ToList();
                    GridView1.DataBind();
                }
            }
            TextBox txtSearch = GridView1.HeaderRow.FindControl("txtfilterName") as TextBox;
            ScriptManager1.RegisterAsyncPostBackControl(txtSearch);
            if (txtSearch == null)
            {
                Response.Write("error");
            }
            else
            {
                Response.Write("Found");
            }
        }

        protected void txtfilterName_TextChanged(object sender, EventArgs e)
        {

            //if (Page.IsPostBack)
            //{



            TextBox txtSearch = GridView1.HeaderRow.FindControl("txtfilterName") as TextBox;
            if (txtSearch == null)
            {
                Response.Write("error");
            }
            Label3.Text = txtSearch.Text;
            Label4.Text = txtSearch.Text;

            using (var dc=new SPISDBOnlineEntities())
            {
                GridView1.DataSource = dc.ViewProjects.Take(5).ToList();
                
                GridView1.DataBind();
                //UpdatePanel1.Update();
            }
        }
            //}
        

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}


please let me know where I went wrong... Many thanks to all :)
Posted
Updated 27-Jan-14 22:25pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900