views:

39

answers:

2

I've been using ASP.NET (C#) for the past two years. I have learned so much but there is still much more to learn :)

I've used MasterPages, 'Web User Controls' for things like headers, navigation, footers etc.

One thing I have never truly understood is the practice of using 'Web User Controls' for your content and logic;

Home.aspx ------ Home.ascx
AboutUs.aspx ----- AboutUs.ascx
Ordering.aspx ---- Ordering.acsx

I've been working with a few projects over the last few months that use this structure. I am aware that this is actually common practice but I don't really understand the full benefits.

I remember when I tried this approach before and ended up having awful viewstate problems with controls like the Gridview... once I took all the logic out and placed it into a .aspx page everything worked fine.

I realise now that maybe I needed to add the Gridview to the viewstate collection... but this only reinforces my difficulty into understanding why this approach is used - given the viewstate problem.

I fully understand the advantages of 'Web User Controls' in relation to things like Headers, menus, footers etc... anywhere that involves duplication but the projects I've seen have pages/controls that are pretty specific - in other words its unlikely to be reused anywhere else - the aspx page just contains a control (.ascx) with the content and logic and it will only be used on that page, nowhere else.

Ignoring 'Code Reuse', what other benefits does this approach provide?

+1  A: 

User controls are used inside Web forms...they are not used in replacement


Edit:

In that case, I don't see any benefit in using that method(besides reuse). It can actually have more overhead, since page cycle events have to be passed from the page to the control.

Ed B
I'm aware of that - my point is you will have page such as Home.aspx and all that contains is a custom control called Home.ascx ... Home.aspx is almost like a wrapper... I'm trying to understand why this approach is used especially in cases where there is no requirement for code reuse.
Dal
In that particular example there isn't actually any real benefit to using a web user control. It sounds like Masterpages would have been more appropriate.
Brian Scott
@Ed B - That's exactly my predicament ;)
Dal
+1  A: 

I think your example of PageA.aspx -> PageA.ascx is actually a bad example for highlighting the benefits of web user controls. Take a step back from these "page" control examples and think of the main purpose of a web user control. They serve the purpose of encapsulating UI and functionality within a discrete block.

The biggest benefit in my opinion is in knowing that if I develop something like a comments form once as a web user control and deploy it throughout my site pages that I have a single item to update in the solution which will update every page it exists upon.

Another less obvious benefit is that many Content Management systems such as Umbraco use the web user control as plugins. That way you can develop plugins for the end user / editor to select and place into page content as they see fit.

Brian Scott
Brian, placing something like a comment form in a web control makes total sense, I'm all for placing widgets like this in controls... maybe these projects that I have seen where the whole page is nested inside a web control isn't the way to do things after all? .... if Home.aspx contained multiple controls like 'HomepageSlideshow.ascx', 'TwitterFeed.ascx' this would make much more sense.
Dal
@Dal, exactly. Although technically different a page and a web user control essentially behave very very similiarly and there needs to be a real purpose in order for the approach you are describing to have been taken. Without further detail there's no obvious benefit
Brian Scott