2012-01-23 44 views
2

我在我的網站中有幾個DropDownList,他們正在過濾對方。過濾DropDownList不會工作

所以,我有一所學校,基於我有班級,而在那個班裏面我有學生。每個人在數據庫上都有自己的表格,並且它是從具有所有ID的表格生成的。

我不知道爲什麼,但我可以過濾學校的班級,但學生DropdownList不會受到過濾器的影響。

這是我的代碼:

<li>School </li> 
<li> 
    <asp:DropDownList ID="SchoolBox" runat="server" AutoPostBack="True" 
     DataSourceID="DropDownlistSchool" DataTextField="SchoolName" 
     DataValueField="ID"> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="DropDownlistSchool" runat="server" 
     ConnectionString="<%$ ConnectionStrings:DanielConnectionString %>" 
     SelectCommand="SELECT [SchoolName], [ID] FROM [Schools]"> 
    </asp:SqlDataSource> 
</li> 
<li>Class</li> 
<li> 
    <asp:DropDownList ID="ClassBox" runat="server" AutoPostBack="True" 
     DataSourceID="Class2" DataTextField="ClassName" DataValueField="ID"> 
    </asp:DropDownList> 

    <asp:SqlDataSource ID="Class2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:DanielConnectionString %>" 
     SelectCommand="SELECT * FROM [Class] WHERE ([SchoolID] = @SchoolID)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="SchoolBox" Name="SchoolID" 
       PropertyName="SelectedValue" Type="Int32" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 

</li> 
<li>Student</li> 
<li> 
    <asp:DropDownList ID="StudentBox" runat="server" AutoPostBack="True" 
     DataSourceID="Student" DataTextField="Username" DataValueField="ID"> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="Student" runat="server" 
     ConnectionString="<%$ ConnectionStrings:DanielConnectionString %>" 

     SelectCommand="SELECT * FROM [Users] WHERE (([ClassID] = @ClassID) AND ([SchoolID] = @SchoolID))"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="ClassBox" Name="ClassID" 
       PropertyName="SelectedValue" Type="Int32" /> 
      <asp:ControlParameter ControlID="SchoolBox" Name="SchoolID" 
       PropertyName="SelectedValue" Type="Int32" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
+0

在代碼背後是否有任何事情發生? –

+0

沒有。在我的代碼中沒有任何反應 – thormayer

+0

當你選擇'學校'後,你的課程被過濾。在類被過濾之後,如果您選擇與過濾器之後顯示的默認類不同的類,會發生什麼情況?你的學生是否被過濾? –

回答

1

您需要將數據綁定事件添加到您的「ClassBox」 DropDownList

<asp:DropDownList ID="ClassBox" runat="server" AutoPostBack="True" 
    DataSourceID="Class2" DataTextField="ClassName" DataValueField="ID" 
    OnDataBound="ClassBox_DataBound" > 
</asp:DropDownList> 

然後,在後面的代碼,你需要做的是:

protected void ClassBox_DataBound(object sender, EventArgs e) 
{ 
    // Bind the SQLDataSource 
    Student.DataBind(); 
    // Re-bind the associated DropDownList 
    StudentBox.DataBind(); 
} 

當你選擇一所學校,出現AutoPostBack,並根據「SchoolBox」的SelectedValue更新「ClassBox」。此時,「ClassBox」還沒有SelectedValue,所以「StudentBox」數據源沒有參數。一旦「ClassBox」是數據綁定的話,重做「StudentBox」的數據綁定(從而獲得新的信息)是安全的。

+0

謝謝!有效! – thormayer

+0

@thormayer沒問題,很樂意幫忙=) – jadarnel27

0

在類&學生下拉你設置:分別

DataValueField="ClassId", DataValueField="StudentId" 

DataValueField="ID" 

如果有這樣的設置。

這是除非你也有表中的Id字段?

+0

謝謝,我沒有「StudentId」,我在這兩個表(學生表和班級表)中都有「ID」。在學生表中我有ClassID,但在Class表中,我只有:ID,ClassName,SchoolID。 – thormayer

0

我有一個類似的形式與級聯DropDownLists,每一個篩選下一個。我發現使用上面提到的方法,它只會在第一次在DropDownList中選擇一個值時工作,但在此之後,我無法再次獲取列表的值。換句話說,當我第一次加載表單併爲第一個DropDownList選擇一個值時,第二個將填充...當我爲第二個選擇一個值時,第三個將填充...但是如果我然後改變了第二個,我無法再獲得第三次重新填充。它總是陷入第一組值。

因此,我構建了一個名爲RebuildDropDown的小函數,我從OnSelectedIndexChanged處理程序中調用該函數。在給定DropDownList的處理函數中,我爲每個從屬列表調用RebuildDropDown,如下所示:

protected void SchoolBox_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    RebuildDropDown(ClassBox); 
    RebuildDropDown(StudentBox); 
} 

protected void ClassBox_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    RebuildDropDown(StudentBox); 
} 

protected void RebuildDropDown(DropDownList ddl) 
{ 
    ddl.Items.Clear(); 

    // OPTIONAL: add "ALL" option as first option 
    ListItem li = new ListItem("All", "ALL"); 
    ddl.Items.Add(li); 

    // now add databound items 
    ddl.DataBind(); 
}