2010-07-12 88 views
0

我有兩個下拉列表(一個動態填充,另一個沒有)確定第三個下拉列表的值。這個想法是,第一個ddl1是強制性的,ddl2是可選的。一旦選擇了ddl1值,如何獲得ddl2的空值(不是值被選中)。謝謝!背後通過兩個獨立(不級聯)的下拉列表動態填充第三個下拉列表

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>" SelectCommand="SELECT Category FROM Categories"></asp:SqlDataSource> 

    <asp:DropDownList ID="DropDownList1" AutoPostBack="True" runat="server" DataSourceID="SqlDataSource1" DataTextField="Category" DataValueField="Category" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
    <asp:ListItem Text="Select category" Value=""/> 
    </asp:DropDownList> 

    <asp:DropDownList ID="DropDownList2" runat="server"> 
    <asp:ListItem Text="All types" Value="" /> 
     <asp:ListItem Value="Policy">Policy</asp:ListItem> 
     <asp:ListItem Value="Form">Form</asp:ListItem> 
     <asp:ListItem Value="Other">Other</asp:ListItem> 
      </asp:DropDownList> 

    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" DataTextField="DocName" DataValueField="DocID" AppendDataBoundItems="True"> 
    <asp:ListItem Text="Select document" Value=""/> 
    </asp:DropDownList> 

代碼:

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 

     DropDownList3.Items.Clear() 
     DropDownList3.Items.Add(New ListItem("Select document", "")) 

     If (DropDownList1.SelectedIndex <> 0 Or DropDownList1.SelectedIndex > 0) Then 
     SqlDataSource3.SelectCommand = "SELECT DocID, DocName, Category, DocType FROM Doc_LibraryTable WHERE (Category = @Cat) AND (DocType = @DocType OR @DocType IS NULL)"    
     Dim controlDdl1 As ControlParameter = New ControlParameter 
     controlDdl1.ControlID = "DropDownList1" 
     controlDdl1.Name = "Cat" 
     controlDdl1.Type = TypeCode.String 
     controlDdl1.PropertyName = "SelectedValue" 
     SqlDataSource3.SelectParameters.Clear() 
     SqlDataSource3.SelectParameters.Add(controlDdl1) 

     Dim controlDdl2 As ControlParameter = New ControlParameter 
     controlDdl2.ControlID = "DropDownList2" 
     controlDdl2.Name = "DocType" 
     controlDdl2.Type = TypeCode.String 
     controlDdl2.PropertyName = "SelectedValue" 
     SqlDataSource3.SelectParameters.Clear() 
     SqlDataSource3.SelectParameters.Add(controlDdl2) 
     SqlDataSource3.SelectParameters("DocType").DefaultValue = "" 


    End If 
End Sub 

回答

1

許多事情改變。

  • 兩個DropDownList1 & DropDownList2應的AutoPostBack = true,並且事件處理程序應該用於兩者。就像你這樣做的,用戶必須選擇DropDownList2然後DropDownList1才能工作。我們希望他們能夠以任意順序選擇它們。

  • 下一行DropDownList1.SelectedIndex <> 0 Or DropDownList1.SelectedIndex > 0是多餘的(任何使下半部分爲真的值都會使前半部分成立)If DropDownList1.SelectedIndex <> 0 Then已足夠。

  • 不要清除參數列表兩次 - 您正在擦除您的第一個參數。

  • 最後,你想要的部分。代碼中不需要使用ControlParameter。它旨在用於您想要在標記中設置關係的情況。如果您使用的代碼,你可以直接指定的值:

    SqlDataSource3.SelectCommand = "SELECT DocID, DocName, Category, DocType " +_ 
               "FROM Doc_LibraryTable "+_ 
               "WHERE (Category = @Cat) "+_ 
               "AND (DocType = @DocType "+_ 
               "OR @DocType IS NULL)" 
    
    SqlDataSource3.SelectParameters.Clear() 
    SqlDataSource3.SelectParameters.Add("Cat", TypeCode.String, _ 
         DropDownList1.SelectedValue) 
    SqlDataSource3.SelectParameters.Add("DocType", TypeCode.String, _ 
         DropDownList2.SelectedValue ?? "") 
    

(PS,我是一個C#的傢伙,所以運氣好的話,我已經得到了延續行權......)

+0

非常感謝您的幫助。我得到它的工作! – netNewbi3 2010-07-13 12:35:20

相關問題