Page view counter

Dynamically loaded user controls.

Last post 07-15-2008 11:40 AM by daniel.saidi. 2 replies.

Sort Posts:

  • Dynamically loaded user controls.

    01-02-2004, 3:56 PM
    • Loading...
    • scorchandburn
    • Joined on 01-02-2004, 3:51 PM
    • Posts 5
    • Points 25
    I have a series of user controls that I am loading dynamically based on selection of a drop down list. The problem I am having is when the second control is dynamically loaded because the user made a different selection from the drop down the first dynamically loaded controls values remain in the second controls fields.

    I am loading the controls in the overridden LoadViewState(object savedState) method. This allows me to retain viewstate of the dynamically loaded control on postback.

    Any ideas as to why this would be happening.

    Thanks!
  • Re: Dynamically loaded user controls.

    01-02-2004, 5:37 PM
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,308
    • Points 30,007
    Every control on the page has a unique ID called the ClientID. (That's an actual property on each control.) A user control is a "naming container". That means each control it contains also contains the ID of the User control. For example, "TextBox1" in UserControl with the ID "UserControl1" will have the clientID:
    UserControl1_TextBox1.

    Post back retrieves the values from each field by the ClientID. For example, to get the above TextBox's value from the form, use Request.Forms["UserControl1_TextBox1"].
    Now what happens when you replace one UserControl with another that has the same ClientID? The second gets the data of the first.

    Recommendations:
    1. Always assign a unique ID to the UserControl in its ID property.
    2. You should always recreate the controls of the page you sent to the browser on postback before rearranging them. That allows the original controls to load their data and run their event handlers. It also allows the ViewState to properly distribute its contents without getting a ViewState corrupted error.

    So on Post_Back, I recommend calling LoadControl to get the original UserControl. Then before loading the second, set the first's Visible property to false to remove it.
    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Dynamically loaded user controls.

    07-15-2008, 11:40 AM
    • Loading...
    • daniel.saidi
    • Joined on 07-15-2008, 11:38 AM
    • Posts 2
    • Points 4

    The previous post was rather old, but the issue with dynamically loaded user controls still remain.

    All dynamically loaded controls are unloaded from the page when a postback occurs. They will thus not remain on the page if you press a button or perform a PostBack in any way. This is quite annoying, since you manually have to reload any dynamically loaded controls to the page after the postback. You must also make sure to reload them at the right time and with the correct ID...otherwise they will not work properly. For instance, events may not fire, nested controls may crasch etc.

    This can become quite a problem for large, complex web applications where dynamically loaded controls are essential to keep down byte size and avoid a lot of unnecessary functionality. A very simple and handy solution to this issue is described in this blog:

    http://daniel-saidi.blogspot.com/2008/07/aspnet-dynamically-loaded-usercontrol.html

    It features a slimmed, downloadable class which, hopefully, will take care of this issue for you.

Page 1 of 1 (3 items)