我有两个下拉列表(一个是动态填充的,另一个不是),它们决定了第三个下拉列表的值。这个想法是第一个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
答案 0 :(得分:1)
有很多事情需要改变。
DropDownList1&amp; 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 ?? "")
(P.S。,我是一个C#家伙,所以运气好的话,我的线路延续了......)