我在我的頁面上有一個列表視圖,我想通過兩個下拉列表過濾,現在我已經實現了列表視圖和控件。通過2個控件過濾gridview(dropdownlists)
我發現的是,兩個控件不能同時工作。第一個控件可以正常工作,但是不管第一個控件是否設置,第二個控件都不會(默認顯示全部)。
有沒有什麼辦法呢?下面我寫了我在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]";
}
}
什麼** **究竟你的意思是兩個控制不能在工作同時 ? – phadaphunk
因此,我們可以說我選擇英國在國家下拉列表中,這將工作。但是,如果我從地區下拉列表中選擇任何內容,無論在國家/地區下拉列表中選擇了什麼內容。如果我從AccessDataSource中取出國家/地區控制參數,區域纔有效。 – Imran