Results 1 to 7 of 7

Thread: [SERIOUS] - Dynamically Loading User Controls in .NET

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    [SERIOUS] - Dynamically Loading User Controls in .NET

    So I was debating with a co-worker the other day and she stated that the "correct" way to implement UserControls in .NET was to dynamically load them, as opposed to just placing them on the page.

    She said it wasn't as "Heavy" and left a "Light foot-print".

    I simply stated "Really? I never heard that before." because I honestly never had. So I'm curious has anyone else heard that dynamically loading usercontrols is the "correct" way to implement them?

    If so can you provide some links where this information is documented.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [SERIOUS] - Dynamically Loading User Controls in .NET

    When you place a control on the webform in design time, take a look at the designer code created for it (assuming you're using VS 2003).

    Now, do the dynamic control creation, and look at your code.

    Both methods are practically the same. The only difference being that if you dynamically load a control, then it gets rendered as the (for example) load event for the page executes, so there is only a displacement of the generation code.

    At the end of the processing, the page and all the objects therein are destroyed anyways.

    It is easy to have this misconception though, as one may think that not placing a control on the form makes it lighter, when in fact, they actually need to understand the web programming paradigm.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [SERIOUS] - Dynamically Loading User Controls in .NET

    That also goes for desktop apps. Adding controls through the designer just generates the same code to add them that you would use yourself anyway.

  4. #4

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Re: [SERIOUS] - Dynamically Loading User Controls in .NET

    Okay, so I'm still not able to determine which way is more efficient.

    1) I created a UserControl, dropped it on the page and stepped through the page events as they fired, but didn't see anything happen so it appears the loading of the control is handled and encapsulated behind the scenes through the pages "Register" tag.

    2) I wrote the code (see below) to dynamically add the user control to a placeholder on the page, the only additional code that was executed was the code I wrote.
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
      Control c = this.LoadControl("ucTest.ascx");
      PlaceHolder1.Controls.Add(c);
    }
    So heres is my assumption. There isn't any obvious difference between loading a user control dynamically and just dropping one on the page. The only difference is you can view the code you wrote to dynamically add it and watch it execute, otherwise the pages "Register" tag handles all of the processing automatically when the user control is manually added to the page.

    Conclusion: No real difference between doing it dynamically or not. Although, there wasn't any visible/traceable "proof" that one was more efficient or "lighter" than the other.

    Anyone else have any opinions on this subject?
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  5. #5

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [SERIOUS] - Dynamically Loading User Controls in .NET

    There is no difference bewteen the two. Any controls that appear on your form or page have to be created and rendered. Where the creation takes place makes no difference. Where dynamically creating and destroying controls may be an advantage is if you won't be displaying all controls on the form at once. In that case it may be more efficient to wait until a control is needed to create it. If it's never used then you've saved the effort of creating it. If it is used then at least you've allowed the page/form to load more quickly at the outset. I don't code Web apps so I'm not sure what all the implications are for ASP.NET, but this is certainly the case for WinForms. If you definitely need a control on a page/form from the outset though, then you save yourself nothing by doing the job in code rather than at design time. As mendhak posted, everything is code anyway. Using the designer doesn't magically create objects with no code. It simply writes the code for you. Why would it matter who writes the code if it will be executed at page load either way?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [SERIOUS] - Dynamically Loading User Controls in .NET

    Think about it this way. The main overhead in a server/client app is the communication. Adding a control dynamically produces the same result, therefore the same data is sent to the client. So, from a bandwidth point of view, there's no difference.

    As for purely on the server side - if you stuff a control tag into the .aspx file, it is picked up and added as the aspx code is parsed. If you add it in the codebehind, it is executed at that point (and you will see it as you step through). Both methods occur as the page is being formed. Now take into account the actual logic flow in your app, plus routine processes such as compilation, caching, etc., and the white noise will quickly obscure any signal of difference there might be.

    Conclusion - Don't lose any sleep over it.

Posting Permissions

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



Click Here to Expand Forum to Full Width