Dynamically Building Controls in ASP.net

Discussion in 'ASP .Net Building Controls' started by D.Dark, Nov 15, 2007.

  1. D.Dark

    D.Dark Guest

    I've seen alot of questions and replies about building controls dynamically
    in ASP.net but i can't find one with answers to my specific situation:

    I'm trying to build controls dynamically based on the user selection from a
    DropDownListBox. I can't automatically build the controls in the Page_Init
    method from the start since I have to wait for the user to choose and option
    from the DropDown. Also, when the user chooses an option from the DropDown it
    posts back and immediately runs the Page_Init method before the
    SelectedIndexChanged for the DropDown (which I need to run to perform the
    processing necessary to determine how many controls needs to be created). And
    by the time it gets to the SelectedIndexChanged method to do the
    calculations, the Page_Init has already run. In addition to that, when the
    user changes their choice in the DropDown and it posts back, the Page_Init is
    seeing the results of the processing from the last selection since the
    SelectedIndexChanged hasn't run yet to recalculate based on the new selection.

    Hopefully someone out there has had a similar experience and can provide so
    insite, Thanks!
    D.Dark, Nov 15, 2007
    #1
    1. Advertising

  2. D.Dark

    Riki Guest

    D.Dark wrote:
    > I've seen alot of questions and replies about building controls
    > dynamically in ASP.net but i can't find one with answers to my
    > specific situation:
    >
    > I'm trying to build controls dynamically based on the user selection
    > from a DropDownListBox. I can't automatically build the controls in
    > the Page_Init method from the start since I have to wait for the user
    > to choose and option from the DropDown. Also, when the user chooses
    > an option from the DropDown it posts back and immediately runs the
    > Page_Init method before the SelectedIndexChanged for the DropDown
    > (which I need to run to perform the processing necessary to determine
    > how many controls needs to be created). And by the time it gets to
    > the SelectedIndexChanged method to do the calculations, the Page_Init
    > has already run. In addition to that, when the user changes their
    > choice in the DropDown and it posts back, the Page_Init is seeing the
    > results of the processing from the last selection since the
    > SelectedIndexChanged hasn't run yet to recalculate based on the new
    > selection.
    >
    > Hopefully someone out there has had a similar experience and can
    > provide so insite, Thanks!


    Your observations are correct.
    In my opinion, the best solution to this is to wait until
    SelectedIndexChanged, and do all the work there.
    If necessary, delete all the controls that have been created in Page_Init,
    and start again.
    (You can put your controls in a placeholder control and run
    myplaceholder.Controls.Clear() to delete them all).
    I admit that this will be a little bit slower, because some of the work will
    be done twice, but on the bright side, you avoid a lot of trouble.

    --

    Riki
    Riki, Nov 19, 2007
    #2
    1. Advertising

  3. D.Dark

    D.Dark Guest

    Thanks for the help. I was already using a placeholder for the controls so i
    could clear them out all at once but I didn't want to have to create a
    predetermined number of controls just to make sure that they were always
    there and then remove the ones that I didn't need later.

    I ended up using a control array to solve my problem. With the control array
    I only created one control in the Page_Init and created the rest of the
    controls in the SelectedIndexChanged event. Since they were all part of the
    same control array with the same name, all the events for the controls wired
    up properly even though they weren't created in the Page_Init. Then I used
    ViewState to keep track of the number of controls that were created the last
    time so I could recreate the same number of controls again when the page
    posts back.

    "Riki" wrote:

    > Your observations are correct.
    > In my opinion, the best solution to this is to wait until
    > SelectedIndexChanged, and do all the work there.
    > If necessary, delete all the controls that have been created in Page_Init,
    > and start again.
    > (You can put your controls in a placeholder control and run
    > myplaceholder.Controls.Clear() to delete them all).
    > I admit that this will be a little bit slower, because some of the work will
    > be done twice, but on the bright side, you avoid a lot of trouble.
    >
    > --
    >
    > Riki
    >
    >
    >
    D.Dark, Nov 20, 2007
    #3
    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. Nish
    Replies:
    1
    Views:
    297
    Scott Mitchell [MVP]
    Nov 13, 2004
  2. =?Utf-8?B?SXJmYW4gQWtyYW0=?=

    Dynamic Controls in asp.net...Building a tree hierarchy..please he

    =?Utf-8?B?SXJmYW4gQWtyYW0=?=, Dec 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    501
    =?Utf-8?B?SXJmYW4gQWtyYW0=?=
    Dec 13, 2004
  3. Bob Rock
    Replies:
    1
    Views:
    626
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  4. Chris Lane
    Replies:
    1
    Views:
    118
    Hermit Dave
    Nov 25, 2003
  5. Nick P

    Dynamically building custom ASP.NET forms

    Nick P, May 11, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    103
    Nick P
    May 11, 2004
Loading...

Share This Page