示例我有5个DropDownList
值
red, orange, yellow, green, blue
如果DropDownList1
选择红色
DropDownList2-5
的选择将是
orange, yellow, green, blue
如果DropDownList2
选择yellow
DropDownList3-5
的选择将是
orange, green, blue
以下是我在互联网上找到的代码,这仅适用于3 DropDownList
,但我需要的是5 DropDownList
。我无法展开5 DropDownList
VB
导入System.Data Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Private bFlag As Boolean = True
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
FillddlLocations()
End If
End Sub
'Properties to store selected value in ViewState
Protected Property MemberID1Selection() As String
Get
If ViewState("MemberID1Selection") IsNot Nothing Then
Return ViewState("MemberID1Selection").ToString()
End If
Return ""
End Get
Set(value As String)
ViewState("MemberID1Selection") = value
End Set
End Property
Protected Property MemberID2Selection() As String
Get
If ViewState("MemberID2Selection") IsNot Nothing Then
Return ViewState("MemberID2Selection").ToString()
End If
Return ""
End Get
Set(value As String)
ViewState("MemberID2Selection") = value
End Set
End Property
Protected Property MemberID3Selection() As String
Get
If ViewState("MemberID3Selection") IsNot Nothing Then
Return ViewState("MemberID3Selection").ToString()
End If
Return ""
End Get
Set(value As String)
ViewState("MemberID3Selection") = value
End Set
End Property
Protected Sub FillddlLocations()
FillDropdown(companyID1)
FillDropdown(companyID2)
FillDropdown(companyID3)
companyID1.Visible = True
companyID2.Visible = True
companyID3.Visible = True
End Sub
Protected Sub FillDropdown(ddl As DropDownList)
Using connAdd = New SqlConnection("Data Source = MENDOZAABBY-PC\SQLEXPRESS; Initial Catalog = ThesisDatabase; Integrated Security= True")
connAdd.Open()
Dim sql = "SELECT CompanyName FROM Company Where College = 'CCS'"
Using cmdAdd = New SqlDataAdapter(sql, connAdd)
Dim ds2 As New DataSet()
cmdAdd.Fill(ds2)
ddl.Items.Clear()
ddl.DataSource = ds2
ddl.DataTextField = "CompanyName"
ddl.DataValueField = "CompanyName"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("Please select a Company", ""))
ddl.SelectedIndex = 0
End Using
End Using
End Sub
Protected Sub IndexChanged(ddlChanged As DropDownList, ddlToFilter1 As DropDownList, ddlToFilter2 As DropDownList)
Dim removeValue1 As String = If(ddlChanged Is companyID1, MemberID1Selection, (If(ddlChanged Is companyID2, MemberID2Selection, MemberID3Selection)))
Dim selValue2 As String = If(ddlChanged Is companyID1, MemberID2Selection, (If(ddlChanged Is companyID2, MemberID1Selection, MemberID1Selection)))
Dim selValue3 As String = If(ddlChanged Is companyID1, MemberID3Selection, (If(ddlChanged Is companyID2, MemberID3Selection, MemberID2Selection)))
bFlag = False
'Prevent fireing the code again while changing the index
If removeValue1 <> "" Then
Dim item1 As ListItem = ddlToFilter1.Items.FindByValue(removeValue1)
ddlToFilter1.Items.Remove(item1)
Dim item2 As ListItem = ddlToFilter2.Items.FindByValue(removeValue1)
ddlToFilter2.Items.Remove(item2)
End If
If selValue3 <> "" Then
Dim item3 As ListItem = ddlToFilter1.Items.FindByValue(selValue3)
ddlToFilter1.Items.Remove(item3)
End If
If selValue2 <> "" Then
Dim item4 As ListItem = ddlToFilter2.Items.FindByValue(selValue2)
ddlToFilter2.Items.Remove(item4)
End If
bFlag = False
ddlToFilter1.SelectedIndex = ddlToFilter1.Items.IndexOf(ddlToFilter1.Items.FindByValue(selValue2))
ddlToFilter2.SelectedIndex = ddlToFilter2.Items.IndexOf(ddlToFilter2.Items.FindByValue(selValue3))
End Sub
Protected Sub ddlpid1_SelectedIndexChanged(sender As Object, e As EventArgs)
MemberID1Selection = companyID1.SelectedValue
If bFlag Then
FillDropdown(companyID2)
FillDropdown(companyID3)
IndexChanged(companyID1, companyID2, companyID3)
End If
End Sub
Protected Sub ddlpid2_SelectedIndexChanged(sender As Object, e As EventArgs)
MemberID2Selection = companyID2.SelectedValue
If bFlag Then
FillDropdown(companyID1)
FillDropdown(companyID3)
IndexChanged(companyID2, companyID1, companyID3)
End If
End Sub
Protected Sub ddlpid3_SelectedIndexChanged(sender As Object, e As EventArgs)
MemberID3Selection = companyID3.SelectedValue
If bFlag Then
FillDropdown(companyID1)
FillDropdown(companyID2)
IndexChanged(companyID3, companyID1, companyID2)
End If
End Sub
结束班
ASPX
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="companyID1" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="companyID2" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid2_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="companyID3" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid3_SelectedIndexChanged" >
</asp:DropDownList>
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
让我给你一个想法,这将有效。创建一个矩阵对象。它可能是一个数组。
Items -> | red | orange | yellow | green |
Controls | | | | |
| | | | | |
V | | | | |
_________|______|________|________|_______|
combo1 |true | false | true |true |
_________|______|________|________|_______|
combo2 |true | false | true |true |
_________|______|________|________|_______|
combo3 |true | false | true |true |
_________|______|________|________|_______|
combo4 |true | true | true |true |
_________|______|________|________|_______|
更新
根据此矩阵,您的界面将包含combo4项目 - 所有其他组合将只有红色,黄色和绿色。
当接下来单击combo3并且例如选择黄色时,您将使用“true”填充单元格“combo3 / yellow”,将其他剩余单元格填充为黄色 - “false”。
Items -> | red | orange | yellow | green |
Controls | | | | |
| | | | | |
V | | | | |
_________|______|________|________|_______|
combo1 |true | false | false |true |
_________|______|________|________|_______|
combo2 |true | false | false |true |
_________|______|________|________|_______|
combo3 |true | false | true |true |
_________|______|________|________|_______|
combo4 |true | true | false |true |
_________|______|________|________|_______|
得到了主意?
现在,开发逻辑,在单击组合时将这些“单元格”设置为false。 一开始,所有颜色都可用于所有控件 - “true”。然后单击一个(任意)组合。您保留该控件/颜色单元格。然后基于此矩阵构建UI。每次点击都会有回发,您的控件将重新填充可用的颜色。
最好的部分 - 您可以为它们提供任意数量的控件或项目。一旦项目用完或没有控制,就无法填写控件:o)