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 reall开发者_Python百科y 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?
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.
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.
精彩评论