所以,我尝试在头中使用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();
}
我错过了什么?
3条答案
按热度按时间wlwcrazw1#
代码中有两个错误。
首先将[Article-Name]列名称放在括号中
第二,如何将数据源分配给网格视图。请参阅下面的正确方法。
km0tfn4u2#
使用http://www.datatables.net/
它提供数据过滤、排序、分页等功能。
只需在gridview上方放置一个html表格标题,并添加一些id即可。
添加添加jquery脚本
mutmk8jj3#
首先,在你的gridview的RowDataBound事件中(查找你的文本框):
之后,使用它的值(任何地方你想要的,我用它到一个按钮点击事件):