How to get value of dynamic DropDownList in User Control ?

Discussion in 'ASP .Net' started by Piotr Strycharz, Sep 29, 2003.

  1. 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.
    Piotr Strycharz, Sep 29, 2003
    #1
    1. Advertising

  2. 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"
    <-SPAMU.pl> wrote in message
    news:bl9bef$8ec$...
    > 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.
    >
    Kevin Spencer, Sep 29, 2003
    #2
    1. Advertising

  3. U¿ytkownik "Kevin Spencer" <> napisa³ w
    wiadomo¶ci news:%...
    > 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.
    Piotr Strycharz, Sep 29, 2003
    #3
  4. 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"
    <-SPAMU.pl> wrote in message
    news:bl9bef$8ec$...
    > 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.
    >
    Chris Jackson, Sep 29, 2003
    #4
  5. Piotr Strycharz

    Tomk20 Guest

    Instead of Request.Form try this:

    ViewState("ControlName")





    "Piotr Strycharz"
    <-SPAMU.pl> wrote in message
    news:bl9hv7$c8q$...
    >
    > U¿ytkownik "Kevin Spencer" <> napisa³ w
    > wiadomo¶ci news:%...
    > > 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, Sep 29, 2003
    #5
  6. "Tomk20" <> wrote in message
    news:...
    > 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
    John Saunders, Sep 29, 2003
    #6
  7. U¿ytkownik "Chris Jackson" <> napisa³ w wiadomo¶ci
    news:...
    > 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.
    Piotr Strycharz, Sep 30, 2003
    #7
  8. 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"
    <-SPAMU.pl> wrote in message
    news:bl9hv7$c8q$...
    >
    > U¿ytkownik "Kevin Spencer" <> napisa³ w
    > wiadomo¶ci news:%...
    > > 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.
    >
    Kevin Spencer, Sep 30, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hb
    Replies:
    2
    Views:
    3,355
    Steven Cheng[MSFT]
    Apr 13, 2004
  2. Replies:
    0
    Views:
    826
  3. Hugo Flores
    Replies:
    1
    Views:
    491
    daniel #
    Sep 6, 2006
  4. Larry Bud
    Replies:
    1
    Views:
    339
    bruce barker
    Jan 10, 2007
  5. E
    Replies:
    2
    Views:
    135
Loading...

Share This Page