woodykiddy
woodykiddy

Reputation: 6455

How to use Web User Controls in ASP.NET efficiently

Currently, I have got 7 user controls placed on a single aspx page, some of which are created as Menu, Footer, or Header, etc.

I am just wondering if it's a good practice to always take advantage of web user controls to separate different page components. Would there be any real impact on the performance, e.g. on page load or server responsiveness, etc, if there's relatively large numbers of controls going to be used on aspx pages?

I have also noticed that @ OutputCache is an option for caching user controls, so is it good idea to apply @ OutputCache for Header, Footer and those kind of user controls?

Upvotes: 0

Views: 734

Answers (1)

Dai
Dai

Reputation: 155145

Premature optimization is the root of all evil

- Donald Knuth.

Do not use output caching, or any other form of optimization unless you have evidence gained from profiling that you need to use it. Output caching is really more to prevent needing to hit a database back-end for commonly-accessed, non-personalized webpages, like a high-traffic frontpage.

There is nothing inherently wrong, performance-wise, with using UserControls - everything gets compiled behind-the-scenes into a high-performance assembly that returns text via an output buffer writer directly. The only "cost" of using a control is an additional vtable call to the Render function, and the extra bytes each control's state uses in-memory. i.e. it's negligible.

During a page's lifecycle, the step that uses the most time is invariably hitting a database or external web-service. Rendering a completed page takes around a millisecond or less. Enable output tracing if you don't believe me.

That said, I question why you're using UserControls for common site areas, like a header and footer - this is what MasterPages are for.

Upvotes: 2

Related Questions