gridview里面有实体框架的更新面板 [英] gridview inside update panel with entity framework

查看:78
本文介绍了gridview里面有实体框架的更新面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有

场景:

i想在我的网格视图中使用实体框架实现过滤功能。

方法:

我在gridview的标题模板中添加了一个文本框,并根据输入文本框的文本过滤数据,方法是在文本框的TextChanged事件中添加过滤方法



测试进行:

在事情变得复杂之前,我想测试我的逻辑所以我做的是在表单加载我选择了表中的所有数据和文本更改事件我只选择前5行,这样就可以明显改变了。



测试结果:

当我调试我的代码时我发现了前5行被分配给gridview数据源,但没有反映在页面上



标记代码:



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:

<%@ 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)
        {

        }
    }
}





请让我知道我哪里出错...非常感谢all:)



please let me know where I went wrong... Many thanks to all :)

推荐答案

请看下面的例子



http://aspsnippets.com/Articles/Filter-GridView-Records-using-DropDownList- in-HeaderTemplate-Header-Row-in-ASPNet.aspx [ ^ ]
Please see below example

http://aspsnippets.com/Articles/Filter-GridView-Records-using-DropDownList-in-HeaderTemplate-Header-Row-in-ASPNet.aspx[^]


这篇关于gridview里面有实体框架的更新面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆