Professional Web Applications Themes

Dynamically Building Controls in ASP.net - ASP.NET Building Controls

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 ...

  1. #1

    Default Dynamically Building Controls in ASP.net

    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 Guest

  2. #2

    Default Re: Dynamically Building Controls in ASP.net

    D.Dark 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


    Riki Guest

  3. #3

    Default Re: Dynamically Building Controls in ASP.net

    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:
     
    D.Dark Guest

Similar Threads

  1. dynamically building template columns
    By jonefer in forum ASP.NET Building Controls
    Replies: 4
    Last Post: May 26th, 06:08 PM
  2. Building Controls advice
    By Griff in forum ASP.NET Building Controls
    Replies: 6
    Last Post: October 11th, 05:01 PM
  3. Example of Building Composite Controls in ASP.NET 2.0
    By Sasha in forum ASP.NET Building Controls
    Replies: 0
    Last Post: May 31st, 06:44 PM
  4. Anybody actually building controls?
    By CMM in forum ASP.NET Building Controls
    Replies: 2
    Last Post: April 18th, 04:20 AM
  5. Dynamically Building Property Values for Designer
    By paul reed in forum ASP.NET Building Controls
    Replies: 8
    Last Post: February 16th, 09:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139