我通过GridView
处理ObjectDataSource
。我还根据dropdown list
以及textbox
来搜索一些过滤函数。我的代码如下所示:
.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中启用了Paging。现在我的问题是:当我选择过滤器并按部门搜索时,我得到了预期的结果。举个例子,我搜索了媒体部门,页面上显示了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 + "' ";
}
}
答案 0 :(得分:0)
我实际上让它与上面的编辑一起工作。它不是最漂亮的解决方案,但它有效 - 只要发布这个答案,万一其他人遇到同样的问题。