Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > problem with selectedvalue of dropdownlist

Reply
Thread Tools

problem with selectedvalue of dropdownlist

 
 
Chris
Guest
Posts: n/a
 
      05-17-2008
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>


 
Reply With Quote
 
 
 
 
Rain
Guest
Posts: n/a
 
      05-17-2008
Put your label setting code in the pre-render event or the index select
changed event for the dropdown box.




"Chris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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>
>



 
Reply With Quote
 
 
 
 
Lloyd Sheen
Guest
Posts: n/a
 
      05-17-2008

"Chris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      05-17-2008
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 ...


"Lloyd Sheen" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
>
> "Chris" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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



 
Reply With Quote
 
Rain
Guest
Posts: n/a
 
      05-17-2008
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 )








"Chris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ...
>
>
> "Lloyd Sheen" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
>>
>> "Chris" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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

>
>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      05-17-2008
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?




"Mark Rae [MVP]" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
> "Rain" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> 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);

>
> 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...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      05-17-2008
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" <(E-Mail Removed)> schreef in bericht
news:usM$(E-Mail Removed)...
>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?
>
>
>
>
> "Mark Rae [MVP]" <(E-Mail Removed)> schreef in bericht
> news:%(E-Mail Removed)...
>> "Rain" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>
>>> 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);

>>
>> 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...
>>
>>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net

>
>



 
Reply With Quote
 
Rain
Guest
Posts: n/a
 
      05-17-2008
Where did I mention doing it in the Page_Load ?


"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Rain" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> 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);

>
> 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...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      05-17-2008
I don't know ...
If you really want to help, please just tell me what's wrong
Thanks


"Rain" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
> Where did I mention doing it in the Page_Load ?
>
>
> "Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> "Rain" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>
>>> 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);

>>
>> 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...
>>
>>
>> --
>> Mark Rae
>> ASP.NET MVP
>> http://www.markrae.net

>
>



 
Reply With Quote
 
Rain
Guest
Posts: n/a
 
      05-18-2008
Cool, I didnt think I had



Cheers


"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Rain" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
> [top-posting corrected]
>
>>>> 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);
>>>
>>> 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...

>>
>> Where did I mention doing it in the Page_Load ?

>
> You didn't - that's how it was in the OP's code...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Binding Problem in DropDownList Control SelectedValue to Table text field Tom Jorgenson ASP .Net 3 03-09-2007 10:42 AM
problem about combination gridview-dropdownlist (and selectedvalue) Ben ASP .Net Datagrid Control 0 08-15-2006 04:22 PM
problem about combination gridview-dropdownlist (and selectedvalue) Ben ASP .Net 0 08-15-2006 04:22 PM
Problem in Setting SelectedValue for DropdownList in Form Charleees ASP .Net Web Controls 2 12-30-2005 01:43 PM
Problem with changing the index of the SelectedValue of a DropDownList Thomas Kern ASP .Net 1 11-09-2004 09:47 AM



Advertisments