Elias Elias - 6 months ago 15
Vb.net Question

How do I get the value of a multiple dynamically generated dropdownlist?

I'm doing a small website for school and I got stuck on this problem. How can I get the value of each dropdownlist? The dropdownlist are dynamically generated by code. I did the same with the textboxes and it works perfectly, but with the values of the dropdownlists doesn't work, I mean, I don't get the values back. What should I do?

Partial Class Opties
Inherits System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim aantalMensen As Integer = CInt(Session("aantalVolwassenen")) + CInt(Session("aantalKinderen")) + CInt(Session("aantalBabys"))
Session("aantalMensen") = CStr(aantalMensen)

For x As Integer = 1 To aantalMensen
'Passagiers info
Dim aanhef As New DropDownList()
Dim aanhefSelectie As ListItem
aanhefSelectie = New ListItem("Dhr.", "Dhr.")
aanhef.Items.Add(aanhefSelectie)
aanhefSelectie = New ListItem("Mevr.", "Mevr.")
aanhef.Items.Add(aanhefSelectie)
Dim voornaam As New TextBox With {.ID = "txtVoornaam" & x}
Dim achternaam As New TextBox With {.ID = "txtAchternaam" & x}


Dim lt As New Literal()
Dim Endlt As New Literal()
Dim space As New Literal()
lt.Text = "<p>Persoon " + CStr(x) + ":"
Endlt.Text = "</p> "
space.Text = "<br />"

gegevensPassagiers.Controls.Add(lt)
gegevensPassagiers.Controls.Add(aanhef)
gegevensPassagiers.Controls.Add(voornaam)
gegevensPassagiers.Controls.Add(achternaam)
gegevensPassagiers.Controls.Add(Endlt)
gegevensPassagiers.Controls.Add(space)
Next

For i As Integer = 1 To aantalMensen
'Bagage DropDownlist
Dim aantalKG As New DropDownList With {.ID = "ddlBagage" & i}
Dim KGselectie As ListItem
KGselectie = New ListItem("Geen", "Geen")
aantalKG.Items.Add(KGselectie)
KGselectie = New ListItem("15kg", "15kg")
aantalKG.Items.Add(KGselectie)
KGselectie = New ListItem("25kg", "25kg")
aantalKG.Items.Add(KGselectie)
KGselectie = New ListItem("35kg", "35kg")
aantalKG.Items.Add(KGselectie)
aantalKG.AutoPostBack = True

Dim lt As New Literal()
Dim Endlt As New Literal()
Dim space As New Literal()
lt.Text = "<p>Persoon " + CStr(i) + ":"
Endlt.Text = "</p> "
space.Text = "<br />"

bagageDIV.Controls.Add(lt)
bagageDIV.Controls.Add(aantalKG)
bagageDIV.Controls.Add(Endlt)
bagageDIV.Controls.Add(space)
Next
Dim index As Integer = 1
If IsPostBack Then
For Each key As String In Request.Form.Keys
If key.Contains("txtVoornaam") Then
Session("Voornaam" & index) = CType(Request.Form(key), String)
index += 1
End If
If key.Contains("txtFamilienaam") Then
Session("Familienaam" & index) = CType(Request.Form(key), String)
index += 1
End If
If key.Contains("ddlBagage") Then
Session("Bagage" & index) = CType(Request.Form(key), String)
index += 1
End If
Next

End If


End Sub

Answer

Well, after testing some methodes and attributes and other stuff.... I found an easy solution for my problem (also thanks for helping @MaCron).

Here is the code:

For Each aantalKG As DropDownList In bagageDIV.Controls.OfType(Of DropDownList)()
  Session("Bagage" & index) = aantalKG.SelectedValue
  index += 1
Next
Comments