473,624 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with selectedvalue of dropdownlist

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.EventArg s) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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>
Jun 27 '08 #1
15 1379

"Chris" <ss*@qsd.dcwrot e in message
news:uT******** ******@TK2MSFTN GP05.phx.gbl...
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.EventArg s) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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 SelectionChange d routine to
pull the information from. Why are you creating the dropdown in code?

LS

Jun 27 '08 #2
Put your label setting code in the pre-render event or the index select
changed event for the dropdown box.


"Chris" <ss*@qsd.dcwrot e in message
news:uT******** ******@TK2MSFTN GP05.phx.gbl...
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.EventArg s) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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>

Jun 27 '08 #3
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 SelectionChange d procedure available ...
"Lloyd Sheen" <a@b.cschreef in bericht
news:uV******** *****@TK2MSFTNG P02.phx.gbl...
>
"Chris" <ss*@qsd.dcwrot e in message
news:uT******** ******@TK2MSFTN GP05.phx.gbl...
>Hi,

When choosing a value in the dropdownlist, i try to put that
selectedvalu e in the label, but i always get the first value 'a'.
EnableViewStat e 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.EventAr gs) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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 SelectionChange d routine to
pull the information from. Why are you creating the dropdown in code?

LS

Jun 27 '08 #4
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.DropDownLi st1.SelectedInd exChanged += new
System.EventHan dler(myFunction Name);

public void myFunctionName( object sender, System.EventArg s e )


"Chris" <ss*@qsd.dcwrot e in message
news:uM******** ******@TK2MSFTN GP02.phx.gbl...
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 SelectionChange d procedure available ...
"Lloyd Sheen" <a@b.cschreef in bericht
news:uV******** *****@TK2MSFTNG P02.phx.gbl...
>>
"Chris" <ss*@qsd.dcwrot e in message
news:uT******* *******@TK2MSFT NGP05.phx.gbl.. .
>>Hi,

When choosing a value in the dropdownlist, i try to put that
selectedval ue in the label, but i always get the first value 'a'.
EnableViewSta te 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.EventA rgs) Handles Me.Load
Dim ddl As New DropDownList
Dim z As ListItem
Dim kz As String
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)

If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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 SelectionChange d routine
to pull the information from. Why are you creating the dropdown in code?

LS


Jun 27 '08 #5
"Rain" <me@myplace.com wrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
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.DropDownLi st1.SelectedInd exChanged += new
System.EventHan dler(myFunction Name);
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

Jun 27 '08 #6
I resolved my problem like this:
Partial Class test
Inherits System.Web.UI.P age
Friend ddl As New DropDownList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load

Dim z As ListItem
Dim kz As String
If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)
End Sub
End Class
Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
ddl.AutoPostBac k = 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]" <ma**@markNOSPA Mrae.netschreef in bericht
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
"Rain" <me@myplace.com wrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>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.DropDownL ist1.SelectedIn dexChanged += new
System.EventHa ndler(myFunctio nName);

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

Jun 27 '08 #7
or maybe better, but still same problem: how to code all the Friend
variables i need ..

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

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) 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(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)
AddHandler ddl.SelectedInd exChanged, AddressOf selectddl
End Sub
Protected Sub selectddl(ByVal sender As Object, ByVal e As
System.EventArg s)
Dim kz As String
kz = ddl.SelectedVal ue
Label1.Text = kz
End Sub
End Class

"Chris" <ss*@qsd.dcschr eef in bericht
news:us******** ******@TK2MSFTN GP03.phx.gbl...
>I resolved my problem like this:
Partial Class test
Inherits System.Web.UI.P age
Friend ddl As New DropDownList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load

Dim z As ListItem
Dim kz As String
If Page.IsPostBack Then
kz = ddl.SelectedVal ue
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(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
ddl.AutoPostBac k = True
form1.Controls. Add(ddl)
End Sub
End Class
Protected Sub Page_PreInit(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreInit
ddl.AutoPostBac k = 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]" <ma**@markNOSPA Mrae.netschreef in bericht
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>"Rain" <me@myplace.com wrote in message
news:%2******* *********@TK2MS FTNGP04.phx.gbl ...
>>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.DropDown List1.SelectedI ndexChanged += new
System.EventH andler(myFuncti onName);

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


Jun 27 '08 #8
Where did I mention doing it in the Page_Load ?
"Mark Rae [MVP]" <ma**@markNOSPA Mrae.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
"Rain" <me@myplace.com wrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>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.DropDownL ist1.SelectedIn dexChanged += new
System.EventHa ndler(myFunctio nName);

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

Jun 27 '08 #9
I don't know ...
If you really want to help, please just tell me what's wrong
Thanks
"Rain" <me@myplace.com schreef in bericht
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Where did I mention doing it in the Page_Load ?
"Mark Rae [MVP]" <ma**@markNOSPA Mrae.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>"Rain" <me@myplace.com wrote in message
news:%2******* *********@TK2MS FTNGP04.phx.gbl ...
>>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.DropDown List1.SelectedI ndexChanged += new
System.EventH andler(myFuncti onName);

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


Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4544
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
2
1465
by: newsgroups.jd | last post by:
Thanks for any advice, help in advance... I have 3 dropdownlist that populate based on choice in the previous (nested essentially) Problem is when I hit the back button and make a change in the second or thrid dropdownlist it does not always redirect to the correct site... What I really want to happen is if the back button is pushed then everything clears and all starts from
3
9840
by: =?Utf-8?B?ZGVuIDIwMDU=?= | last post by:
Hi, Trouble in retaining values of dropdownlist, textboxes, and other controls when dropdownlist selectedindexchanged event is triggered, the controls are inside a user control and this user control inside a parent user control with an update panel. Can you guys help me hwo to retain the values. I have set EnableViewState to true. Where is correct page event to store entered and selected values before the values on controls are...
0
3728
by: andy | last post by:
Hi, I have a form uses several dropdownlists to narrow a set of criteria. ( This is in turn used to control what is shown on a gridview. ) With each, the user selects an entry and then the next dropdownlist uses that control's selected value to drive what it shows. They're all set to autopostback. Everything works fine except where one of the levels only has one entry. This seems to mess up the next level down and you see the wrong
2
9948
by: sree reddy | last post by:
..cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
10
1536
by: Dan | last post by:
Hi, I create 5 dropdownlist in code-behind. I want to insert their selectedvalues in a table by making a string separated with ":" I can get their selecedvalues but i'm stuck when i want to execute the insert. The error is "name dd is not declared" (in line: selvalue = selvalue & Session("dd" & dd.id) & ":" Any idea how to solve this?
0
1468
by: Dan | last post by:
Hi, I have a detailsview with two fields: in editmode, one is a textbox and the other is a dropdownlist. i want to update both fields using the detailsview. My problem: when clicking on the edit button, the modified text in the textbox is effectively updated in the table, but instead of updating the chosen value of the dropdownlist, the field in the table becomes empty (old value is updated by nothing). The items of the dropdownlist...
4
8547
by: pankajsingh5k | last post by:
Hi guys, These question is for all the experts... Please help me before my brain explodes The problem is again with the formview control.. I have a formview and i have to use it that when it displays it shows values from the database and i have a link button edit on the ItemTemplate that changes the mode to edit mode and allows to edit the values which can be edited...
2
6936
by: pankajsingh5k | last post by:
Dear All, Please help me... I had read an article to lazy load a tab in a tabcontainer using an update panel on http://mattberseth.com/blog/2007/07/how_to_lazyload_tabpanels_with.html and i am implementing it in my website....
6
4269
by: shashi shekhar singh | last post by:
Respected Sir, I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist boxes are generating a postback.here is a code . protected void Page_Load(object sender, EventArgs e) { int selected_question = (int)Session; if (!Page.IsPostBack) { display_blueprint(); string...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.