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

Different DropDownLists created at runtime do not allow selecting different items?

我需要在运行时创建许多下拉列表,并且 select 每个下拉列表都有一个不同的项目。 为了避免连续访问数据库,我创建了一个下拉列表,我将其中的项目复制到克隆的下拉列表中。奇怪的是,所有下拉列表都创建了 select 最后一个下拉列表的项目,我不明白为什么!我必须插入 selected 项目 ["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

好的,我自己搞定了! 当我这样做时

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

我无意中在源下拉列表和目标下拉列表之间创建了一个绑定。 要中断绑定,我必须在两个控件之间使用桥梁,在这种情况下,我通过在 2 个连续步骤中设置 2 个属性来使用 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

我说的对吗?