我的页面上有一个列表视图,我想通过两个下拉列表进行过滤,现在我已经实现了listview和控件。
我发现两个控件不能同时工作。第一个控件可以正常工作,但是第二个控件无论是否设置了第一个控件都是无效的(默认为全部显示)。
这有什么办法吗?下面我编写了我在VS中使用的代码以及C#代码。
Visual Studio
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/ASPNetDB.mdb"
SelectCommand="SELECT * FROM [Library]">
<SelectParameters>
<asp:ControlParameter ControlID="SideContent:DropDownList1" Name="Category"
PropertyName="SelectedValue" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="SideContent:DropDownList2" Name="Region"
PropertyName="SelectedValue" Type="String" DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
Category:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="AccessDataSource2" DataTextField="CatName"
DataValueField="CatID" AppendDataBoundItems="true" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0" Selected ="True" >All Categories</asp:ListItem>
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/ASPNetDB.mdb" SelectCommand="SELECT * FROM [CategoryTable]">
</asp:AccessDataSource>
Region:
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="AccessDataSource3" DataTextField="RegionName"
DataValueField="RegionID" AppendDataBoundItems="true" AutoPostBack="true"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Value="0" Selected ="True" >All Regions</asp:ListItem>
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/ASPNetDB.mdb"
SelectCommand="SELECT * FROM [RegionsTable]">
</asp:AccessDataSource>
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var Category = DropDownList1.SelectedValue;
int intCategory = Convert.ToInt16(Category);
if (intCategory> 0)
{
AccessDataSource1.SelectCommand = "SELECT * FROM [CategoryTable] WHERE ([Category] = ?)";
}
else
{
AccessDataSource1.SelectCommand = "SELECT * FROM [CategoryTable]";
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
var Region = DropDownList2.SelectedValue;
int intRegion = Convert.ToInt16(Region);
if (intRegion > 0)
{
AccessDataSource1.SelectCommand = "SELECT * FROM [RegionTable] WHERE ([Region] = ?)";
//Response.Write(intRegion);
}
else
{
AccessDataSource1.SelectCommand = "SELECT * FROM [RegionTable]";
}
}
答案 0 :(得分:1)
C#:
int category = 0, region = 0;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var Category = DropDownList1.SelectedValue;
int intCategory = Convert.ToInt16(Category);
category = intCategory;
if (category > 0)
{
if (region > 0)
{
AccessDataSource1.SelectCommand = "SELECT * FROM [CategoryTable] WHERE ([Category] = ?) AND ([Region]) = ?";
}
else
{
AccessDataSource1.SelectCommand = "SELECT * FROM [CategoryTable] WHERE ([Category] = ?)";
}
}
else
{
AccessDataSource1.SelectCommand = "SELECT * FROM [CategoryTable]";
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
var Region = DropDownList2.SelectedValue;
int intRegion = Convert.ToInt16(Region);
region = intRegion;
if (region > 0)
{
if (category > 0)
{
AccessDataSource1.SelectCommand = "SELECT * FROM [RegionTable] WHERE ([Region] = ?) AND ([Category]) = ?";
}
else
{
AccessDataSource1.SelectCommand = "SELECT * FROM [RegionTable] WHERE ([Region] = ?)";
}
//Response.Write(intRegion);
}
else
{
AccessDataSource1.SelectCommand = "SELECT * FROM [RegionTable]";
}
}