Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The original problem is how to make the most maintainable code given that I need to have a menu bar that is very similar but not identical in most WebForms.

At first glance I thought I wanted to make a placeholder in a custom user control, which was placed in a master page, then access the placeholder, putting in the unique content within the WebForm and all is happy and maintainable. After reading through the forms and through trial and error, I realize there are some problems with every solution I have tried:

  1. Putting a placeholder control in a user control makes the placeholder and each of its contents difficult to access in the web form (relying on nested instances of FindControl("ID")), events are difficult to bubble up (I assume I have to do this when the control is created in the code behind where it is referenced) or bind (not successful yet despite referencing: http://msdn.microsoft.com/en-us/library/t4z863dh(vs.71).aspx), and the code is difficult to maintain if I want to move/rename the control.
  2. Nesting controls gives the same problem because I want to use one control - making small modifications on many pages. If I nest then I still have to have cross-control knowledge of each other which is difficult in ASP.NET. Regardless, if I nested user controls then I would also need a unique user control per WebForm right?
  3. Copying and pasting the code to each WebForm while simple is also repetitive and error prone.
  4. Apparently, I shouldn't even try to inherit user controls to make unique children: How (if at all) can you make an ASP.NET UserControl inherit from another UserControl?.

I'm new to ASP.NET. Is there some general strategy I should use to make good maintainable code in this scenario?

share|improve this question
    
Have you tried exposing inner controls as properties of their containers? Way easier to access them like that, no need to fetch by ID. Also, inheritance is done just like with any other classes in the framework. –  Renan May 8 '13 at 20:33
    
Why don't you use a site map style solution? Put your menu(s) in a XML file you'll parse (just reference the right one in each page). You can even have a "master" XML file for common menu items (to merge, for example with XSLT). –  Adriano Repetti May 8 '13 at 20:36

1 Answer 1

You need to use a Master Page, and put your menu in there...

http://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx

Then, for a data-driven menu, try to use one of the template controls like the Repeater to bind your data.

For example, I have a query that returns Page Name and Link - I bind this to a Repeater control to produce my Menu.

<asp:Repeater runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate><li><a href='<% Eval("Link") %>'><%# Eval("PageName") %></a></li></ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
share|improve this answer
    
Using a master page is what I ended up doing. I got confused when I needed to add unique content to each web form. –  haleonj Jun 12 '13 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.