我有一个看起来像这样的gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="10"
DataKeyNames="id"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnPageIndexChanged="GridView1_PageIndexChanged"
AllowPaging="True">
工作正常。我想在 gridview 上方添加一个下拉框,作为数据库结果的过滤器:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Value="0">Show all</asp:ListItem>
<asp:ListItem Value="1">In queue</asp:ListItem>
<asp:ListItem Value="2">Being worked on</asp:ListItem>
<asp:ListItem Value="3">Complete</asp:ListItem>
<asp:ListItem Value="4">Declined</asp:ListItem>
</asp:DropDownList>
如何让下拉菜单与 gridview 交互以相应地更新结果?请记住,gridview 已启用分页,因此单击页面时应记住下拉选项,并且还应根据页码和下拉选择记住数据库结果。
更新:
我按如下方式填充网格视图,其中 BindData 进入 Page_Load:
private void BindData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connString"]);
SqlDataAdapter da = new SqlDataAdapter(@"sql query here", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Page_Load 看起来像这样:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}