asp.net 筛选标题中带有文本框的网格视图

pw9qyyiw  于 2022-11-19  发布在  .NET
关注(0)|答案(3)|浏览(131)

所以,我尝试在头中使用textboxes来过滤我的gridview。我为每个textbox创建了一个textchanged方法。现在我想过滤gridview并绑定过滤后的数据。由于某种原因,一旦datasource命中textchanged方法,它就为null。请看下面的代码:

<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();
        }

我错过了什么?

wlwcrazw

wlwcrazw1#

代码中有两个错误。
首先将[Article-Name]列名称放在括号中
第二,如何将数据源分配给网格视图。请参阅下面的正确方法。

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();
km0tfn4u

km0tfn4u2#

使用http://www.datatables.net/
它提供数据过滤、排序、分页等功能。
只需在gridview上方放置一个html表格标题,并添加一些id即可。

<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();
});
mutmk8jj

mutmk8jj3#

首先,在你的gridview的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;

相关问题