2012-11-29 43 views
0

我在我的页面上有一个列表视图,我想通过两个下拉列表过滤,现在我已经实现了列表视图和控件。通过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]"; 
    } 

} 
+0

什么** **究竟你的意思是两个控制不能在工作同时 ? – phadaphunk

+0

因此,我们可以说我选择英国在国家下拉列表中,这将工作。但是,如果我从地区下拉列表中选择任何内容,无论在国家/地区下拉列表中选择了什么内容。如果我从AccessDataSource中取出国家/地区控制参数,区域才有效。 – Imran

回答

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

    } 
+0

所以这段代码更多地处理了如果两个下拉列表都发生了变化,这不是我遇到的问题。问题的关键在于DropDownList2不会过滤结果,它与DropDownList2的代码没有任何关系,因为我已经尝试为DropDownList1取出ListView的控制参数,然后DropDownList2正常工作。 – Imran