访问多个动态下拉列表选择的值 - ASP.Net

时间:2012-08-12 20:03:42

标签: asp.net vb.net

我在表单中添加了多个动态下拉列表。 我想点击一个按钮访问下拉列表的选定值。

            Private pnlDropDownList As Panel
                Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
                    'Create a Dynamic Panel 
                    pnlDropDownList = New Panel()
                    pnlDropDownList.ID = "pnlDropDownList"
                    pnlDropDownList.BorderWidth = 1
                    pnlDropDownList.Width = 300
                    Me.form1.Controls.Add(pnlDropDownList)

                    'Create a LinkDynamic Button to Add TextBoxes 
                    Dim btnAddDdl As New Button
                    btnAddDdl.ID = "btnAddDdl"
                    btnAddDdl.Text = "Add DropDownList"
                    AddHandler btnAddDdl.Click, AddressOf btnAdd_Click
                    Me.form1.Controls.Add(btnAddDdl)

                    'Recreate Controls
                    RecreateControls("ddlDynamic", "DropDownList")
                End Sub

                Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
                    Dim cnt As Integer = FindOccurence("ddlDynamic")
                    CreateDropDownList("ddlDynamic-" & Convert.ToString(cnt + 1))
                End Sub

                Private Function FindOccurence(ByVal substr As String) As Integer
                    Dim reqstr As String = Request.Form.ToString()
                    Return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length)
                End Function

                Private Sub RecreateControls(ByVal ctrlPrefix As String, ByVal ctrlType As String)
                    Dim ctrls As String() = Request.Form.ToString().Split("&"c)
                    Dim cnt As Integer = FindOccurence(ctrlPrefix)
                    If cnt > 0 Then
                        For k As Integer = 1 To cnt
                            For i As Integer = 0 To ctrls.Length - 1
                                If ctrls(i).Contains((ctrlPrefix & "-") + k.ToString()) AndAlso Not ctrls(i).Contains("EVENTTARGET") Then
                                    Dim ctrlID As String = ctrls(i).Split("="c)(0)

                                    If ctrlType = "DropDownList" Then
                                        CreateDropDownList(ctrlID)
                                    End If
                                    Exit For
                                End If
                            Next
                        Next
                    End If
                End Sub

                Private Sub CreateDropDownList(ByVal ID As String)

                    Dim ddl As New DropDownList()
                    ddl.ID = ID
                    ddl.DataSource = Me.odsNames
                    ddl.DataTextField = "Name"
                    ddl.DataValueField = "ID"
                    ddl.DataBind()
                    ddl.Items.Insert(0, New ListItem("All", -1))
                    ddl.SelectedIndex = 0
                    ddl.AutoPostBack = True
                    AddHandler ddl.SelectedIndexChanged, AddressOf OnSelectedIndexChanged
                    pnlDropDownList.Controls.Add(ddl)


                    Dim lt As New Literal()
                    lt.Text = "<br />"
                    pnlDropDownList.Controls.Add(lt)
                End Sub

                Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
                    'Not used, want to get Values from button click
                End Sub


            Protected Sub cmdAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdAdd.Click
                                    Dim cnt As Integer = FindOccurence("ddlDynamic")
                                    pnlDropDownList.Controls.Remove(pnlDropDownList.FindControl("ddlDynamic-" & Convert.ToString(cnt)))
            End Sub

这是我想检查所有动态下拉列表并提取其选定值的地方

                Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
                    '' Check All dynamic Dropdownlists and retrieve selected values
                End Sub


            End Class

非常感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

您可以使用Linq在面板中获取动态DropDowns的参考:

Dim allDdls = pnlDropDownList.Controls.OfType(Of DropDownList)()
For Each ddl In allDdls
    Dim selectedValue = ddl.SelectedValue
Next