在运行时创建的不同DropDownLists不允许选择其他项?

时间:2019-11-23 09:56:56

标签: drop-down-menu copy runtime items

我需要在运行时创建许多下拉列表,并为每个选择一个不同的项目。 为了避免连续访问数据库,我创建了一个下拉列表,将其复制到克隆的下拉列表中。奇怪的是,所有创建的下拉列表都选择了上一个下拉列表中的项目,我不明白为什么!我必须插入所选项目的清洗[“ DlistClone.ClearSelection()”],因为否则代码会出错。谁能帮我?谢谢

   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyRow As New TableRow
    Dim MyCell As New TableCell

    Dim DList As DropDownList
    Dim DlistClone As DropDownList
    DList = New DropDownList
    DList.Items.Add("1")
    DList.Items.Add("2")
    DList.Items.Add("3")

    MyRow = New TableRow
    MyCell = New TableCell
    DlistClone = New DropDownList
    ClonaDList(DList, DlistClone)
    DlistClone.ClearSelection()
    DlistClone.Items.FindByText("3").Selected = True
    MyCell.Controls.Add(DlistClone)
    MyRow.Cells.Add(MyCell)

    Table1.Rows.Add(MyRow)

    MyRow = New TableRow
    MyCell = New TableCell
    DlistClone = New DropDownList
    ClonaDList(DList, DlistClone)
    DlistClone.ClearSelection()
    DlistClone.Items.FindByText("2").Selected = True
    MyCell.Controls.Add(DlistClone)
    MyRow.Cells.Add(MyCell)

    Table1.Rows.Add(MyRow)
End Sub

Sub ClonaDList(ByVal Origine As DropDownList, ByVal Destinazione As DropDownList)
    Dim I As Integer
    Dim EleList As ListItem

    For I = 0 To Origine.Items.Count - 1
        EleList = Origine.Items(I)
        Destinazione.Items.Add(EleList)
    Next I
End Sub

1 个答案:

答案 0 :(得分:0)

好的,我自己弄的! 当我这样做

  

EleList = Origin.Items(I)
  Destination.Items.Add(EleList)

我在源Dropdownlist和目标Dropdownlist之间创建了意外绑定。 要中断绑定,我必须在两个控件之间建立一个桥梁,在这种情况下,我将通过两个连续的步骤设置两个属性来使用Listitem对象

Sub ClonaDList(ByRef Origine As DropDownList, ByRef Destinazione As DropDownList)
    Dim I As Integer
    Dim EleList As ListItem

    For I = 0 To Origine.Items.Count - 1
        EleList = New ListItem
        EleList.Text = Origine.Items(I).Text
        EleList.Value = Origine.Items(I).Value
        Destinazione.Items.Add(EleList)
    Next I
End Sub

我说的对吗?