0

我的页面上有一个列表视图,我想通过两个下拉列表进行过滤,现在我已经实现了列表视图和控件。

我发现两个控件不能同时工作。第一个控件可以正常工作,但是无论是否设置了第一个控件(默认显示全部),第二个控件都不会。

有没有办法解决这个问题?下面我编写了我在 VS 中使用的代码以及 C# 代码。

视觉工作室

    <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]";
    }

}
4

1 回答 1

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]";
        }

    }
于 2012-11-30T06:36:34.557 回答