By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,467 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,467 IT Pros & Developers. It's quick & easy.

How to get value of dynamic DropDownList in User Control ?

P: n/a
Hi all.

I have problem with ASP:DropDownList. I do not know how to get its value.
The DropDownList is populated in Page_Load method (from SQL Server). When
this list is embeded in ASPX page everything runs fine: I can use
Request.Form["DDL_Name"] to know its value.
However, when I use it in control (ASCX) the framework adds something like
"ctl5_:" to the name of the control. So Request.Form[] method does not work.
What is NOT working too, is the SelectedIndexChanged event. This is because
the number of list items is zero at first, so the framework always sets
SelectedIndex to 0.

How to deal with it ?

Regards.

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Controls which are added dynamically must be re-added and re-populated with
each PostBack. Make sure that you are aware of the Control Execution
Lifecycle when you do this, as the Control must be re-added prior to
processing events. Here is a link to a Microsoft resource on the Control
Execution Lifecycle:

http://msdn.microsoft.com/library/de...nlifecycle.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

"Piotr Strycharz"
<Pi**********************@computerplus.krakow.BE Z-SPAMU.pl> wrote in message
news:bl**********@nemesis.news.tpi.pl...
Hi all.

I have problem with ASP:DropDownList. I do not know how to get its value.
The DropDownList is populated in Page_Load method (from SQL Server). When
this list is embeded in ASPX page everything runs fine: I can use
Request.Form["DDL_Name"] to know its value.
However, when I use it in control (ASCX) the framework adds something like
"ctl5_:" to the name of the control. So Request.Form[] method does not work. What is NOT working too, is the SelectedIndexChanged event. This is because the number of list items is zero at first, so the framework always sets
SelectedIndex to 0.

How to deal with it ?

Regards.

Nov 17 '05 #2

P: n/a

Użytkownik "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> napisał w
wiadomości news:%2****************@TK2MSFTNGP10.phx.gbl...
Controls which are added dynamically must be re-added and re-populated with each PostBack. Make sure that you are aware of the Control Execution
Lifecycle when you do this, as the Control must be re-added prior to
processing events. Here is a link to a Microsoft resource on the Control
Execution Lifecycle:


Well, the controls are not ADDED dynamically (my fault in description), just
the values are. Controls are present at Load. The values are not.

The controls' items are loaded based on the knowledge of values of other
lists. Eg: when I choose "processor" in one control, another control can
have values of "AMD", "Intel", but certainly not "sweet", "salty". However I
cannot generate the other control, because I don't know yet the value of the
previous other controls. Each of the DDLs does depend on the others.
This could be done using Form[...]. However Form[....] does not work in
UserControls.
This cannot be done using .SelectedValue property - because the controls are
not populated yet.

I'm stuck.

Regards.

Piotr.

Nov 17 '05 #3

P: n/a
You don't want to use Request.Form anyway - it defeats the purpose of the
object oriented framework to do that. ASP Classic forced you too, but those
days are finally over. Just use DropDownList.SelectedItem or
DropDownList.SelectedValue, depending on your needs.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
"Piotr Strycharz"
<Pi**********************@computerplus.krakow.BE Z-SPAMU.pl> wrote in message
news:bl**********@nemesis.news.tpi.pl...
Hi all.

I have problem with ASP:DropDownList. I do not know how to get its value.
The DropDownList is populated in Page_Load method (from SQL Server). When
this list is embeded in ASPX page everything runs fine: I can use
Request.Form["DDL_Name"] to know its value.
However, when I use it in control (ASCX) the framework adds something like
"ctl5_:" to the name of the control. So Request.Form[] method does not work. What is NOT working too, is the SelectedIndexChanged event. This is because the number of list items is zero at first, so the framework always sets
SelectedIndex to 0.

How to deal with it ?

Regards.

Nov 17 '05 #4

P: n/a
Instead of Request.Form try this:

ViewState("ControlName")

"Piotr Strycharz"
<Pi**********************@computerplus.krakow.BE Z-SPAMU.pl> wrote in message
news:bl**********@atlantis.news.tpi.pl...

Użytkownik "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> napisał w
wiadomości news:%2****************@TK2MSFTNGP10.phx.gbl...
Controls which are added dynamically must be re-added and re-populated with
each PostBack. Make sure that you are aware of the Control Execution
Lifecycle when you do this, as the Control must be re-added prior to
processing events. Here is a link to a Microsoft resource on the Control
Execution Lifecycle:


Well, the controls are not ADDED dynamically (my fault in description),

just the values are. Controls are present at Load. The values are not.

The controls' items are loaded based on the knowledge of values of other
lists. Eg: when I choose "processor" in one control, another control can
have values of "AMD", "Intel", but certainly not "sweet", "salty". However I cannot generate the other control, because I don't know yet the value of the previous other controls. Each of the DDLs does depend on the others.
This could be done using Form[...]. However Form[....] does not work in
UserControls.
This cannot be done using .SelectedValue property - because the controls are not populated yet.

I'm stuck.

Regards.

Piotr.

Nov 17 '05 #5

P: n/a
"Tomk20" <to****@hotmail.com> wrote in message
news:u4*************@TK2MSFTNGP10.phx.gbl...
Instead of Request.Form try this:

ViewState("ControlName")


That won't work at all. The ViewState saved by a DropDownList is much more
complicated than just its value.
--
John Saunders
Internet Engineer
jo***********@surfcontrol.com
Nov 17 '05 #6

P: n/a

Użytkownik "Chris Jackson" <ch****@mvps.org> napisał w wiadomości
news:er*************@tk2msftngp13.phx.gbl...
You don't want to use Request.Form anyway - it defeats the purpose of the
object oriented framework to do that. ASP Classic forced you too, but those days are finally over. Just use DropDownList.SelectedItem or
DropDownList.SelectedValue, depending on your needs.


O dear...
This is the third time I say: I have NO access to these properties, because
the DropDownLists at Page.Load are EMPTY. These values are "" and 0.
Also I cannot load them before I read their values.

Regards.

Piotr.

Nov 17 '05 #7

P: n/a
It's the same principle. In fact, a ListItem is a Control as well. So, you
need to re-add the ListItems to the DropDownList prior to processing the
Events.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

"Piotr Strycharz"
<Pi**********************@computerplus.krakow.BE Z-SPAMU.pl> wrote in message
news:bl**********@atlantis.news.tpi.pl...

Użytkownik "Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> napisał w
wiadomości news:%2****************@TK2MSFTNGP10.phx.gbl...
Controls which are added dynamically must be re-added and re-populated with
each PostBack. Make sure that you are aware of the Control Execution
Lifecycle when you do this, as the Control must be re-added prior to
processing events. Here is a link to a Microsoft resource on the Control
Execution Lifecycle:


Well, the controls are not ADDED dynamically (my fault in description),

just the values are. Controls are present at Load. The values are not.

The controls' items are loaded based on the knowledge of values of other
lists. Eg: when I choose "processor" in one control, another control can
have values of "AMD", "Intel", but certainly not "sweet", "salty". However I cannot generate the other control, because I don't know yet the value of the previous other controls. Each of the DDLs does depend on the others.
This could be done using Form[...]. However Form[....] does not work in
UserControls.
This cannot be done using .SelectedValue property - because the controls are not populated yet.

I'm stuck.

Regards.

Piotr.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.