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> 15 1364
"Chris" <ss*@qsd.dcwrote in message
news:uT**************@TK2MSFTNGP05.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.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
Put your label setting code in the pre-render event or the index select
changed event for the dropdown box.
"Chris" <ss*@qsd.dcwrote in message
news:uT**************@TK2MSFTNGP05.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.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>
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" <a@b.cschreef in bericht
news:uV*************@TK2MSFTNGP02.phx.gbl...
>
"Chris" <ss*@qsd.dcwrote in message
news:uT**************@TK2MSFTNGP05.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.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
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" <ss*@qsd.dcwrote in message
news:uM**************@TK2MSFTNGP02.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 SelectionChanged procedure available ...
"Lloyd Sheen" <a@b.cschreef in bericht
news:uV*************@TK2MSFTNGP02.phx.gbl...
>> "Chris" <ss*@qsd.dcwrote in message news:uT**************@TK2MSFTNGP05.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.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
"Rain" <me@myplace.comwrote in message
news:%2****************@TK2MSFTNGP04.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.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
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]" <ma**@markNOSPAMrae.netschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Rain" <me@myplace.comwrote in message
news:%2****************@TK2MSFTNGP04.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.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
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" <ss*@qsd.dcschreef in bericht
news:us**************@TK2MSFTNGP03.phx.gbl...
>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]" <ma**@markNOSPAMrae.netschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>"Rain" <me@myplace.comwrote in message news:%2****************@TK2MSFTNGP04.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.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
Where did I mention doing it in the Page_Load ?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Rain" <me@myplace.comwrote in message
news:%2****************@TK2MSFTNGP04.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.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
I don't know ...
If you really want to help, please just tell me what's wrong
Thanks
"Rain" <me@myplace.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Where did I mention doing it in the Page_Load ?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>"Rain" <me@myplace.comwrote in message news:%2****************@TK2MSFTNGP04.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.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
"Rain" <me@myplace.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
[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
"Chris" <ss*@qsd.dcwrote in message
news:us**************@TK2MSFTNGP03.phx.gbl...
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?
I think you're making life difficult for yourself...
In Pre_Init, work out how many dynamic DropDownLists you need and simply add
them to the webform's control collection...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Cool, I didnt think I had
;-)
Cheers
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:e7**************@TK2MSFTNGP04.phx.gbl...
"Rain" <me@myplace.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
[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
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" <ss*@qsd.dcwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...
>I don't know ...
If you really want to help, please just tell me what's wrong
Thanks
"Rain" <me@myplace.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Where did I mention doing it in the Page_Load ?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>>"Rain" <me@myplace.comwrote in message news:%2****************@TK2MSFTNGP04.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.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
"Rain" <me@myplace.comwrote in message
news:eT**************@TK2MSFTNGP06.phx.gbl...
*** [top-posting corrected again] ***
>>Where did I mention doing it in the Page_Load ?
You didn't - that's how it was in the OP's code...
Cool, I didnt think I had
No problem.
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Many thanks.
"Rain" <me@myplace.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
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" <ss*@qsd.dcwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...
>>I don't know ... If you really want to help, please just tell me what's wrong Thanks
"Rain" <me@myplace.comschreef in bericht news:%2****************@TK2MSFTNGP04.phx.gbl...
>>Where did I mention doing it in the Page_Load ?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:%2****************@TK2MSFTNGP03.phx.gbl.. . "Rain" <me@myplace.comwrote in message news:%2****************@TK2MSFTNGP04.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.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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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;
|
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...
|
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...
|
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...
|
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
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
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...
| |