Page view counter

Master Pages for User Controls.... nocandu?

Last post 09-24-2008 1:01 AM by gilfink. 6 replies.

Sort Posts:

  • Master Pages for User Controls.... nocandu?

    09-19-2008, 11:32 AM
    • Loading...
    • BenjaminRay
    • Joined on 10-27-2004, 6:37 PM
    • Posts 4
    • Points 5

    In order to have a consistent look & feel for user controls in a web application, I used to make all user controls inherit from a parent user control.  In the parent user control I overrode the Render sub, inserted the first half of the template HTML, called "MyBase.Render(writer)", and then inserted the second half of the template HTML.  This meant that every user control had the same border, title format, background, etc. - whatever format I wanted.  (I usually used 2 or 3 different parent templates.)

    Now that I'm using ASP.Net 2.0 I thought I could assign a Master Page to a user control instead of forcing it to inherit a parent control with a rigged Render sub.  It seemed simple - I'd insert a placeholder, dynamically add the user controls (like I used to do), and the user controls would all follow the same format.  (In my case, it's usually just a common html table with a formatted title and nice-looking borders - nothing fancy.)

    It turns out that wasn't to be.  How can I accomplish this without having to have my user controls inherit a parent control with a rigged Render?  If I can't assign a Master Page to a user control, can't I at least dynamically add a Web Form Content (.aspx) page to a placeholder?

    Thanks,
    Ben

  • Re: Master Pages for User Controls.... nocandu?

    09-20-2008, 2:39 AM
    • Loading...
    • gilfink
    • Joined on 08-31-2008, 4:36 AM
    • Posts 265
    • Points 2,105

    Hi,
    First, There is no concept of Master Pages for User Controls.
    Master Page is meant to be a template for pages.
    User Controls are a part of a page and therefore can be inserted to the a Master Page but can't have Master Page inside them.
    You can do the following thing:
    Build the Master Page with your User Controls and put the consistent look & feel for user controls in the Master Page (meaning the common html table with a formatted title and nice-looking borders wrapping the user controls).

    I Hope it helps.
    Gil Fink
    MCPD Enterprise Applications Developer
    http://blogs.microsoft.co.il/blogs/gilf
  • Re: Master Pages for User Controls.... nocandu?

    09-21-2008, 1:48 AM
    • Loading...
    • BenjaminRay
    • Joined on 10-27-2004, 6:37 PM
    • Posts 4
    • Points 5

    Thanks for your reply.

    The only issue with your solution is that the controls are being added dynamically.  So the page being displayed uses the Master page, and has a placeholder for the user controls.  I want each of the user controls to inherit the same "template" (i.e. border, title, some variables, etc.), but there could be any number of different types of controls.

    My "old" solution would obviously still work - having a "parent" user control with the rigged Render sub, and all other controls inherit it.  But that was a work-around I made in .Net 1 - I didn't think I'd still have to do it in 2.0.

  • Re: Master Pages for User Controls.... nocandu?

    09-22-2008, 10:39 PM
    • Loading...
    • cool2000
    • Joined on 12-25-2006, 1:55 AM
    • Posts 100
    • Points 20

    NO KIDDING! I wish there were such things as "Master Controls" so that each control that you would put would look the same.

    One thing I can think of is to use themes. (I assume you know about those). For example, your "parent" user control has the following markup:

    <div class="UserControlContainer">
       <h3 class="UserControlHeader">
          <asp:Label id="Header_Label" runat="server"/>
       </h3>
       <div class="UserControlContent">
          <asp:Literal id="Content_LIT" runat="server" />
       </div>
    </div>

    I think you said that you're using the HTMLTextWriter to write the above code, which is fine. It's just that you'll have to tell the HTMLTextWriter to render the class attributes. Now, when you create themes (in the App_Themes folder), you can create CSS files to "render" the divs and the h3 (using the class names mentioned int he code above) with all sorts of border colors, types, background colors, fonts, etc.. This solution [for now] doesn't even touch Master Pages.

    -----------------------------------------------------------------------------------------

    Now, for example, we all of a sudden want to have rounded corners. For that to happen (while we're still in CSS 2.x), we must add a lot to the HTML markup to accomodate the graphics for the four corners. We will have to rewrite our Render function, which is probably what we don't want to do. CSS can only go so far...

    So unfortunately, what we want to do is still a nocandu. I hope enough people petition for Master Controls for the ASP.net 4.0 or earlier.

    I wish we can just create a control (MyControl C = new MyControl();) and then assign it a MasterControl:(C.MasterControl = "~/MasterControls/MasterControl.masterc";).

    I hope somebody posts a solution here. 

  • Re: Master Pages for User Controls.... nocandu?

    09-23-2008, 1:10 PM
    Answer
    • Loading...
    • Dave Sussman
    • Joined on 06-17-2002, 11:53 AM
    • UK
    • Posts 1,960
    • Points 13,947
    • ASPInsiders
      TrustedFriends-MVPs

    I sort of have a solution for this, although rather annoyingly the master user control doesn't work in desgn view, which takes away some of the sparkle. But it does work at runtime and a) I'm a little to busy to sort it out (I'm sure it's simple) and b) I don't use the designer 8)

     Anyway, it's at http://ipona.com/temp/UserControlMaster.zip if you want it; a class library containing the base classes for the user controls and containers and a web site showing it working. Follow up here then others can benefit.

    Dave

     

  • Re: Master Pages for User Controls.... nocandu?

    09-23-2008, 9:54 PM
    • Loading...
    • maxi_ng
    • Joined on 09-03-2008, 5:38 AM
    • Posts 76
    • Points 41

    If I can't assign a Master Page to a user control, can't I at least dynamically add a Web Form Content (.aspx) page to a placeholder?

     

    adding a web form content to a placeholder is what i am trying to do.

    do you know how to achieve that? can you tell me how?

  • Re: Master Pages for User Controls.... nocandu?

    09-24-2008, 1:01 AM
    • Loading...
    • gilfink
    • Joined on 08-31-2008, 4:36 AM
    • Posts 265
    • Points 2,105

    Hi,
    You can't add web form content to a placeholder control but you can use controls like IFrame to embed a form inside another form.

    I Hope it helps.
    Gil Fink
    MCPD Enterprise Applications Developer
    http://blogs.microsoft.co.il/blogs/gilf
Page 1 of 1 (7 items)