正在筛选标头中包含文本框的网格视图



Soo我正在尝试用标题中的textboxes过滤我的gridview。我为每个textbox创建了一个textchanged方法。现在我想过滤gridview并绑定过滤后的数据。由于某种原因,一旦datasource达到textchanged方法,它就为空。请参阅下面的代码:

<asp:GridView ID="GridView_Imported" runat="server" CssClass="GridView-Upload" Width="100%" OnRowDataBound="GridView_Imported_RowDataBound" HorizontalAlign="Center" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="checkAll" runat="server" CssClass="checkAll" onclick="checkAll(this);" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkCtrl" runat="server" CssClass="chkCtrl" onclick="Check_Click(this)" />
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField AccessibleHeaderText="filter">
                    <HeaderTemplate>
                        <asp:Label ID="lbArticleName" runat="server" Text="Article-Name:" CssClass="Article-Name-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterArticleName" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticleName_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="ArticleNameText" runat="server" Text='<%# Eval("Article-Name") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lbArticle" runat="server" Text="Article:" CssClass="Article-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterArticle" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticle_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbArticleText" runat="server" Text='<%# Eval("Article") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lbWarehouse" runat="server" Text="Warehouse:" CssClass="Warehouse-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterWarehouse" runat="server" placeholder="Filter..." OnTextChanged="tbFilterWarehouse_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbWarehouseText" runat="server" Text='<%# Eval("Warehouse") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lbLocation" runat="server" Text="Location:" CssClass="Location-Label"></asp:Label>
                        <br/>
                        <asp:TextBox ID="tbFilterLocation" runat="server" placeholder="Filter..." OnTextChanged="tbFilterLocation_TextChanged" AutoPostBack="True"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbLocationText" runat="server" Text='<%# Eval("Location") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>
        protected void tbFilterArticleName_TextChanged(object sender, EventArgs e)
        {
            var tb = (TextBox) sender as TextBox;
            GridView_Imported.DataSource = (Session["dataSource"] as DataTable).DefaultView.RowFilter = string.Format("Article-Name LIKE '%{0}%'", tb.Text.Trim());

            GridView_Imported.DataBind();
        }

我缺少什么?

您的代码中有两个错误。

首先将列名"文章名称"放在括号中作为[文章名称]。

其次是将数据源分配给gridview的方式。请参阅下面的正确方法。

        var dt = (Session["dataSource"] as DataTable);
        dt.DefaultView.RowFilter = string.Format("[Article-Name] LIKE '%{0}%'", tb.Text.Trim());
        GridView_Imported.DataSource = dt;
        GridView_Imported.DataBind();

使用http://www.datatables.net/

它提供数据过滤、排序、分页等

只需在你的网格视图上方放置一个带有一些id 的html表格标题

<HeaderTemplate>
  <table id="tblData" border="0" cellpadding="5" cellspacing="1" width="100%">
    <thead>
       <th align="center" width="3%">S.No.&nbsp;
       </th>
       <th align="center" width="10%">RFQ ID &nbsp;
       </th>
    </thead>
</HeaderTemplate>

添加添加jquery脚本

$(document).ready(function () {
    $('#tblData').DataTable();
});

首先,在网格视图的RowDataBound事件中(查找文本框):

    System.Web.UI.WebControls.TextBox tbFilterArticle;
    protected void GridView_Imported_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView_Imported.HeaderRow != null)
        {
            tbFilterArticle = (System.Web.UI.WebControls.TextBox)GridView_Imported.HeaderRow.FindControl("tbSearchUser");
        }
    }

在那之后,要使用它的值(无论你想在哪里,我都会将它用于按钮点击事件):

string article = tbFilterArticle.Text;

相关内容

  • 没有找到相关文章

最新更新