problem with selectedvalue of dropdownlist

C

Chris

Hi,

When choosing a value in the dropdownlist, i try to put that selectedvalue
in the label, but i always get the first value 'a'.
EnableViewState is by default set to true, so ... what's wrong in my code?
Thanks
Chris

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBack = True
form1.Controls.Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedValue
Label1.Text = kz
Else
z = New ListItem("a", "a")
ddl.Items.Add(z)
z = New ListItem("b", "b")
ddl.Items.Add(z)
z = New ListItem("c", "c")
ddl.Items.Add(z)
End If
End Sub


<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" ></asp:Label>
</form>
 
R

Rain

Put your label setting code in the pre-render event or the index select
changed event for the dropdown box.
 
L

Lloyd Sheen

Chris said:
Hi,

When choosing a value in the dropdownlist, i try to put that selectedvalue
in the label, but i always get the first value 'a'.
EnableViewState is by default set to true, so ... what's wrong in my code?
Thanks
Chris

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBack = True
form1.Controls.Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedValue
Label1.Text = kz
Else
z = New ListItem("a", "a")
ddl.Items.Add(z)
z = New ListItem("b", "b")
ddl.Items.Add(z)
z = New ListItem("c", "c")
ddl.Items.Add(z)
End If
End Sub


<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" ></asp:Label>
</form>

You are recreating the dropdownlist every time you select an item. Since
you have AutoPostBack on you should be using a SelectionChanged routine to
pull the information from. Why are you creating the dropdown in code?

LS
 
C

Chris

Thanks for replying.
I don't know in advance how many dropdownlists i need. This is coming from a
database, so i have to create them by code.
Doing like this, there is no SelectionChanged procedure available ...
 
R

Rain

Yes there is

You can create an event handler to service the event. Have a look in the
help for adding event handlers. Its a fairly simple one to do. This is the
C# equiv, ( For VB lookup the Addhandler in help ).

this.DropDownList1.SelectedIndexChanged += new
System.EventHandler(myFunctionName);

public void myFunctionName( object sender, System.EventArgs e )
 
C

Chris

I resolved my problem like this:
Partial Class test
Inherits System.Web.UI.Page
Friend ddl As New DropDownList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim z As ListItem
Dim kz As String
If Page.IsPostBack Then
kz = ddl.SelectedValue
Label1.Text = kz
Else
z = New ListItem("a", "a")
ddl.Items.Add(z)
z = New ListItem("b", "b")
ddl.Items.Add(z)
End If
End Sub

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit
ddl.AutoPostBack = True
form1.Controls.Add(ddl)
End Sub
End Class
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit
ddl.AutoPostBack = True
form1.Controls.Add(ddl)
End Sub

But this generates a new problem to me: how can i program that in code?
Assume i need 5 dropdownlists: i can create dim ddl() and then redim
ddl(number needed). But how to do that with Friend, which must be outside
all sub? Or is there a better way to do this?
 
C

Chris

or maybe better, but still same problem: how to code all the Friend
variables i need ..

Partial Class test
Inherits System.Web.UI.Page
Friend ddl As New DropDownList

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim z As ListItem
If Not Page.IsPostBack Then
z = New ListItem("a", "a")
ddl.Items.Add(z)
z = New ListItem("b", "b")
ddl.Items.Add(z)
End If
End Sub

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit
ddl.AutoPostBack = True
form1.Controls.Add(ddl)
AddHandler ddl.SelectedIndexChanged, AddressOf selectddl
End Sub
Protected Sub selectddl(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim kz As String
kz = ddl.SelectedValue
Label1.Text = kz
End Sub
End Class





Chris said:
I resolved my problem like this:
Partial Class test
Inherits System.Web.UI.Page
Friend ddl As New DropDownList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim z As ListItem
Dim kz As String
If Page.IsPostBack Then
kz = ddl.SelectedValue
Label1.Text = kz
Else
z = New ListItem("a", "a")
ddl.Items.Add(z)
z = New ListItem("b", "b")
ddl.Items.Add(z)
End If
End Sub

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit
ddl.AutoPostBack = True
form1.Controls.Add(ddl)
End Sub
End Class
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit
ddl.AutoPostBack = True
form1.Controls.Add(ddl)
End Sub

But this generates a new problem to me: how can i program that in code?
Assume i need 5 dropdownlists: i can create dim ddl() and then redim
ddl(number needed). But how to do that with Friend, which must be outside
all sub? Or is there a better way to do this?
 
R

Rain

Where did I mention doing it in the Page_Load ?


Mark Rae said:
Remember that dynamic controls which need to have events wired up need to
be created in Page_Init or Page_PreInit - Page_Load is usually too late in
the page cycle...
 
R

Rain

This works ( tested ) , hopefully it will help you in formulating your
solution.

--------------------
Private ddls As New ArrayList


Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit

If (Not IsPostBack) Then
Dim myFirstDDl As New DropDownList
myFirstDDl.Items.Add(New ListItem("a", "a"))
myFirstDDl.Items.Add(New ListItem("b", "b"))
myFirstDDl.Items.Add(New ListItem("c", "c"))
myFirstDDl.AutoPostBack = True


'add to ddls array list
ddls.Add(myFirstDDl)

'Save between postbacks
Session("ddls") = ddls
Else
'retreive on postback
ddls = CType(Session("ddls"), ArrayList)

End If

For Each d As DropDownList In ddls

'add to page controls
form1.Controls.Add(d)
AddHandler d.SelectedIndexChanged, AddressOf
handleDDlIndexChanged
Next


End Sub

Protected Sub handleDDlIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)

Label1.Text = ddl.SelectedValue

End Sub












-----------------------------------------------
Chris said:
I don't know ...
If you really want to help, please just tell me what's wrong
Thanks
 
C

Chris

Many thanks.


Rain said:
This works ( tested ) , hopefully it will help you in formulating your
solution.

--------------------
Private ddls As New ArrayList


Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit

If (Not IsPostBack) Then
Dim myFirstDDl As New DropDownList
myFirstDDl.Items.Add(New ListItem("a", "a"))
myFirstDDl.Items.Add(New ListItem("b", "b"))
myFirstDDl.Items.Add(New ListItem("c", "c"))
myFirstDDl.AutoPostBack = True


'add to ddls array list
ddls.Add(myFirstDDl)

'Save between postbacks
Session("ddls") = ddls
Else
'retreive on postback
ddls = CType(Session("ddls"), ArrayList)

End If

For Each d As DropDownList In ddls

'add to page controls
form1.Controls.Add(d)
AddHandler d.SelectedIndexChanged, AddressOf
handleDDlIndexChanged
Next


End Sub

Protected Sub handleDDlIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)

Label1.Text = ddl.SelectedValue

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top