VB.NET - getting the selected item of dynamically created dropdown

Asked By rama chandran on 29-Aug-11 10:17 AM
hi,

actually i given like this, but for 1 dropdown it is ok, the selected item is displaying. but i want to display 5 dropdown and for the 5 dropdown the selecteditem should display in label in next next line. that is i want to loop through. how to do that.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ComboBox As DropDownList = New DropDownList()


ComboBox.ID = "ComboBox"

ComboBox.AutoPostBack = True

ComboBox.Items.Add(New ListItem("Year: 2010", "2010"))

ComboBox.Items.Add(New ListItem("Year: 2011", "2011"))

ComboBox.Items.Add(New ListItem("Year: 2012", "2012"))

ComboBox.Items.Add(New ListItem("Year: 2013", "2013"))

ComboBox.Items.Add(New ListItem("Year: 2014", "2014"))

AddHandler ComboBox.SelectedIndexChanged, AddressOf Dynamic_Method

Me.form1.Controls.Add(ComboBox)

End Sub

Private Sub Dynamic_Method(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim ComboBox As DropDownList = CType(sender, DropDownList)

Dim sSTR As String = ""

sSTR = "</br>Selected Index: " + ComboBox.SelectedIndex.ToString()

sSTR += "</br>Selected Item: " + ComboBox.SelectedItem.Text.ToString()
sSTR += "</br>Selected Value: " + ComboBox.SelectedItem.Value.ToString()

Dim lbl As Label = New Label()

lbl.Text = sSTR

End Sub















Peter Bromberg replied to rama chandran on 29-Aug-11 02:17 PM
I don't see where you are adding these dynamically created labels to your Form

typically you would do something like

Dim lbl As Label = New Label()

lbl.Text = sSTR 
Me.form1.Controls.Add(lbl)
rama chandran replied to Peter Bromberg on 29-Aug-11 11:53 PM
for label ok. pls see what i am asked. give the exact solution and code.