selectedindex after dropdownlist is added to placeholder

Discussion in 'ASP .Net Building Controls' started by Michael Tanner, Jan 20, 2004.

  1. I have created a custom control that derives from the PlaceHolder control.
    Based on a property setting, I load a DropDownList or a TextBox into the
    PlaceHolder control. I load the DropDownList or TextBox control in
    protected override void OnPreRender(EventArgs e).

    The issue that I have is getting the SelectedIndex from the DropDownList
    after a postback. The PlaceHolder control does not have the SelectedIndex
    property and I can't figure out how to maintain the DropDownList after a
    postback - so FindControl doesn't work for me.

    Any ideas would be appreciated.
     
    Michael Tanner, Jan 20, 2004
    #1
    1. Advertising

  2. Hi Michael,

    Note that adding the child controls at PreRender may be too late, as several
    key event (Init, Load, viewstate loading,e tc) had already been fired and
    your added control wasn't there at the proper time. If you're writing a
    composite custom control then CreateChildControls is the place where to
    create child controls.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Michael Tanner" <> wrote in message
    news:...
    > I have created a custom control that derives from the PlaceHolder control.
    > Based on a property setting, I load a DropDownList or a TextBox into the
    > PlaceHolder control. I load the DropDownList or TextBox control in
    > protected override void OnPreRender(EventArgs e).
    >
    > The issue that I have is getting the SelectedIndex from the DropDownList
    > after a postback. The PlaceHolder control does not have the SelectedIndex
    > property and I can't figure out how to maintain the DropDownList after a
    > postback - so FindControl doesn't work for me.
    >
    > Any ideas would be appreciated.
    >
    >
     
    Victor Garcia Aprea [MVP], Jan 22, 2004
    #2
    1. Advertising

  3. I'm having a similar problem on PostBack for a DropDownList.

    I have a page called ManualAdd2.aspx
    That page contains instances of two user controls:
    HeaderBlock.ascx and ctlBrowse.ascx

    HeaderBlock.ascx also contains an instance of ctlBrowse.ascx

    ctlBrowse contains a DropDownList and a TextBox.

    When I run ManualAdd2.aspx, the instance of ctlBrowse that is contained
    within the HeaderBlock control holds state for the DropDownlist in it's
    copy of ctlBrowse just fine.

    The instances of ctlBrowse that just reside in ManualAdd2.aspx DO NOT
    hold their state, however.

    I've looked at the declarations of the control instances and cannot find
    a single difference in declaration.

    Any ideas?


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Nathanael Schulte, Jan 23, 2004
    #3
  4. Victor,

    Thanks for you reply. I found CreateChildControl just a few hours after I
    posted.

    Thanks again,
    Michael

    "Victor Garcia Aprea [MVP]" <> wrote in message
    news:...
    > Hi Michael,
    >
    > Note that adding the child controls at PreRender may be too late, as

    several
    > key event (Init, Load, viewstate loading,e tc) had already been fired and
    > your added control wasn't there at the proper time. If you're writing a
    > composite custom control then CreateChildControls is the place where to
    > create child controls.
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    >
    > "Michael Tanner" <> wrote in message
    > news:...
    > > I have created a custom control that derives from the PlaceHolder

    control.
    > > Based on a property setting, I load a DropDownList or a TextBox into the
    > > PlaceHolder control. I load the DropDownList or TextBox control in
    > > protected override void OnPreRender(EventArgs e).
    > >
    > > The issue that I have is getting the SelectedIndex from the DropDownList
    > > after a postback. The PlaceHolder control does not have the

    SelectedIndex
    > > property and I can't figure out how to maintain the DropDownList after a
    > > postback - so FindControl doesn't work for me.
    > >
    > > Any ideas would be appreciated.
    > >
    > >

    >
    >
     
    Michael Tanner, Jan 28, 2004
    #4
  5. Hey,

    I had a similar issue to yours and maybe your problem is not the same,
    but I'll share what fixed it for me anyway:

    What I was finding was that on postback, my DropDownList would always to
    back to the first entry, no matter what I picked. I pulled up the page
    in the browser then went to View Source in IE. What I noticed was that
    the Value for the rendered <OPTION> control was the same for every
    entry. In other words, then I populated the DropDownList from a
    database, the SelectedItem.Value was the same for every entry in the
    drop down list, so it would always go back to the first entry in the
    list and the SelectedIndexChanged event would never fire.
    So, I would recommend making sure there is a unique value for every
    entry in the DropDownList.

    Hope this helps.

    Thanks,
    thanie2001

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Nathanael Schulte, Jan 28, 2004
    #5
    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. Matthew
    Replies:
    1
    Views:
    1,478
    Matthew
    Nov 12, 2003
  2. =?Utf-8?B?SmFzb24gQ2hhbg==?=

    DropDownList.SelectedIndex doesn't work after Databind

    =?Utf-8?B?SmFzb24gQ2hhbg==?=, Nov 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,851
    Eliyahu Goldin
    Nov 22, 2005
  3. keithb
    Replies:
    1
    Views:
    12,011
    shauns1
    Nov 1, 2006
  4. S_K
    Replies:
    5
    Views:
    771
    Eliyahu Goldin
    Nov 14, 2007
  5. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    345
Loading...

Share This Page