页面出现GridView筛选器问题

本文关键字:筛选 问题 GridView | 更新日期: 2023-09-27 18:22:19

我正在通过ObjectDataSource开发GridView。我还用dropdown listtextbox实现了一些用于搜索的过滤函数。我的代码如下:

.aspx:

   <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False"  AllowPaging="true"  PageSize="30" EmptyDataText="No data available" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="ObjectDataSource1" >
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="" >
            <HeaderStyle Width="45%" />
            <ItemStyle HorizontalAlign="Center" Height="100px" Width="40%" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
            </asp:BoundField>
            <asp:BoundField DataField="Department" HeaderText="Department" ReadOnly="True" SortExpression="" >
                <ItemStyle Width="40%" HorizontalAlign="Center" Height="100px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
                </asp:BoundField>
         </Columns>
  </asp:GridView>
         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="Staff" SelectMethod="StaffData" />

过滤方式:

protected void Button1_Click(object sender, EventArgs e)
{
    if (DropDownList1.SelectedValue.ToString() == "Name")
    {
        ObjectDataSource1.FilterExpression = "Name LIKE '%" + TextBox1.Text + "%' ";
    }
    else if (DropDownList1.SelectedValue.ToString() == "Department")
    {
        ObjectDataSource1.FilterExpression = "Department LIKE '" + TextBox1.Text + "' ";
    }

正如你所看到的,我已经在GridView中启用了分页。现在我的问题是:例如,当我选择过滤器并按部门搜索时,我会得到预期的结果。因此,作为一个例子,我搜索了媒体部门,页面上显示了30个结果。底部的分页指示器显示有4页结果。如果我现在点击第二页,我会进入GridView中所有结果的第二页。因此,无论出于何种原因,过滤器都会被移除。希望我解释得好。有人知道我该怎么解决这个问题吗?

编辑我试着让它像这样与PageIndexChanging一起工作:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    if (ViewState["test"] != null)
    {
        if ((bool)ViewState["test"] == true)
        {
            if (DropDownList1.SelectedValue.ToString() == "Name")
            {
                ObjectDataSource1.FilterExpression = "Name LIKE '%" + TextBox1.Text + "%' ";
            }
            else if (DropDownList1.SelectedValue.ToString() == "Department")
            {
                ObjectDataSource1.FilterExpression = "Department LIKE '" + TextBox1.Text + "' ";
            }
        }
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    ViewState.Add("test", true);
    if (DropDownList1.SelectedValue.ToString() == "Name")
    {
        ObjectDataSource1.FilterExpression = "Name LIKE '%" + TextBox1.Text + "%' ";
    }
 else if (DropDownList1.SelectedValue.ToString() == "Department")
            {
                ObjectDataSource1.FilterExpression = "Department LIKE '" + TextBox1.Text + "' ";
            }
  }

页面出现GridView筛选器问题

我实际上是用它来进行上述编辑的。这不是最漂亮的解决方案,但它很有效——只要发布这个答案,以防其他人遇到同样的问题。