2012-11-29 64 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