我正在为学校做一个小网站而且我遇到了这个问题。如何获得每个下拉列表的值?下拉列表由代码动态生成。我对文本框做了同样的工作并且它完美地工作,但是由于下拉列表的值不起作用,我的意思是,我没有得到这些值。我该怎么办?
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
答案 0 :(得分:0)
您可以使用JavaScript获取下拉列表的选定值。 例如:
var e = document.getElementById("aanhef ");
var Selectedvalue = e.options[e.selectedIndex].value;
答案 1 :(得分:0)
那么,在测试了一些方法和属性以及其他东西之后....我找到了一个简单的解决方案来解决我的问题(也感谢帮助@MaCron)。
以下是代码:
For Each aantalKG As DropDownList In bagageDIV.Controls.OfType(Of DropDownList)()
Session("Bagage" & index) = aantalKG.SelectedValue
index += 1
Next