为了比标题更具描述性,我在ASP.NET中有一系列下拉列表,这些下拉列表都填充了同一过程中的相同值.我正在try 找出一种方法来从后续下拉列表中删除值,前提是该值已在上一个列表中被选中.例如,如果用户在"DDL 1"中 Select "Value 1",则"Value 1"不应出现在"DDL 2"或任何其他后续列表(DDL 3、DDL 4等)中. 现在,我的列表由相同的过程填充,我还有另一个过程试图过滤先前 Select 的值.
下拉列表:
<td>
<asp:DropDownList ID="ddlEquipmentType1" runat="server" AppendDataBoundItems="true" AutoPostBack="true" Width="200px" />
<asp:TextBox runat="server" name="txtIndoorUnit1SerialNumber" Placeholder="Serial #" MaxLength="20" ID="txtIndoorUnit1SerialNumber" Style="width: 200px;" errorName="<%$ Resources:Locale, Global_IndoorUnit1SerialNumber%>" />
<span runat="server" id="vldReqIndoorUnit1SerialNumber" style="color: Red;" Visible="False">*</span>
</td>
</tr>
<tr id="trIndoorUnit2SerialNumber" runat="server" visible="false">
<td style="vertical-align:top">
<asp:label runat="server" id="Label30" Text="<%$ Resources:Locale, Global_IndoorUnit2SerialNumber%>" />
</td>
<td>
<asp:DropDownList ID="ddlEquipmentType2" runat="server" AppendDataBoundItems="true" AutoPostBack="true" Width="200px" />
<asp:TextBox runat="server" name="txtIndoorUnit2SerialNumber" Placeholder="Serial #" MaxLength="20" ID="txtIndoorUnit2SerialNumber" Style="width: 200px;" errorName="<%$ Resources:Locale, Global_IndoorUnit2SerialNumber%>" />
<span runat="server" id="vldReqIndoorUnit2SerialNumber" style="color: Red;" Visible="False">*</span>
</td>
</tr>
(还有更多,但它们都是这样设置的)
填充程序:
Protected Sub PopEquipmentDDLs()
Dim dt As DataTable = New DataTable
Dim strSQL As String = "SELECT RecID, EquipmentType FROM EquipmentTypes WHERE RecID IN (SELECT PrimaryEType FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType1 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType2 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType3 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType4 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType5 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType6 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType7 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType8 FROM ETRelationship WHERE RecID = @RecID UNION ALL SELECT AccessoryEType9 FROM ETRelationship WHERE RecID = @RecID) AND EquipmentType <> @PEquipmentType"
Dim ddlCollection As New List(Of DropDownList)() From {ddlEquipmentType1, ddlEquipmentType2, ddlEquipmentType3, ddlEquipmentType4, ddlEquipmentType5, ddlEquipmentType6, ddlEquipmentType7, ddlEquipmentType8, ddlEquipmentType9}
Dim selectedValues As New Dictionary(Of Integer, String)()
'For i As Integer = 0 To ddlCollection.Count - 1
' Dim ddl As DropDownList = ddlCollection(i)
' If ddl.SelectedIndex > 0 Then
' selectedValues.Add(i, ddl.SelectedItem.Value)
' End If
'Next
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("WillisConnectionString").ToString)
Using cmdSQL As New SqlCommand(strSQL, conn)
cmdSQL.Parameters.Add("RecID", SqlDbType.Int).Value = ddlNbZones.SelectedValue
cmdSQL.Parameters.Add("PEquipmentType", SqlDbType.VarChar).Value = txtEType.Text
conn.Open()
dt.Load(cmdSQL.ExecuteReader())
'Clear all drop-down lists
For Each ddl As DropDownList In ddlCollection
ddl.Items.Clear()
ddl.Items.Add(New ListItem("---SELECT EQUIPMENT---", "0"))
Next
'populate drop-downs
For i As Integer = 0 To ddlCollection.Count - 1
Dim ddl As DropDownList = ddlCollection(i)
ddl.DataSource = dt
ddl.DataTextField = "EquipmentType"
ddl.DataValueField = "RecID"
ddl.DataBind()
'If selectedValues.ContainsKey(i) Then
' ddl.SelectedValue = selectedValues(i)
'End If
Next
For Each item In ddlEquipmentType2.Items
System.Diagnostics.Debug.WriteLine(item)
Next
End Using
End Using
End Sub
过滤程序:
Private Sub FilterDDLs(ByVal ddlCollection As List(Of DropDownList))
For i As Integer = 0 To ddlCollection.Count - 2
Dim ddl As DropDownList = ddlCollection(i)
' Skip filtering if selected index is 0
If ddl.SelectedIndex > 0 Then
Dim selectedItemText As String = ddl.SelectedItem.Text
For j As Integer = i + 1 To ddlCollection.Count - 1
Dim subsequentDDL As DropDownList = ddlCollection(j)
Dim itemToRemove As ListItem = subsequentDDL.Items.FindByText(selectedItemText)
If itemToRemove IsNot Nothing Then
subsequentDDL.Items.Remove(itemToRemove)
End If
Next
End If
Next
End Sub
我从下拉列表的数据绑定事件中调用Filter过程,它循环遍历所有列表,并try 基于匹配文本删除项我也try 基于选定的值删除它们,因为它们都是相同的值.
如果我删除"If ddl.SelectedIndex>;0 Then"并且不查找索引大于0的DDL,它将删除我在填充过程中添加的值为(0)的ListItem,但无论出于什么原因,我都无法让它删除值1+.
我不确定我是不是在错误的位置调用了这个函数,但我也try 了在"seltedIndexChanged"事件中调用过滤器过程,但都没有成功.
我还try 过过滤填充过程中的列表,然后在每个"seltedIndexChanged"事件中调用POP过程,而不是使用单独的过程.因此,注释代码将所选值存储在字典中,然后在事后重新分配它们.
如能就此问题提供任何帮助,将不胜感激.