How to get value of dynamic DropDownList in User Control ?

  • Thread starter Thread starter Piotr Strycharz
  • Start date Start date
P

Piotr Strycharz

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.
 
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/d...guide/html/cpconcontrolexecutionlifecycle.asp

--
HTH,

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

"Piotr Strycharz"
 
U¿ytkownik "Kevin Spencer said:
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.
 
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.
 
Instead of Request.Form try this:

ViewState("ControlName")





"Piotr Strycharz"
U¿ytkownik "Kevin Spencer said:
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.
 
Tomk20 said:
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.
 
U¿ytkownik "Chris Jackson said:
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.
 
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"
U¿ytkownik "Kevin Spencer said:
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.
 
Back
Top