Use masters or not?

Last post 11-01-2006 10:45 PM by Robert_Sitter. 5 replies.

Sort Posts:

  • Use masters or not?

    11-01-2006, 6:12 PM

    Hi there,
    I'm making my first asp.net site and I am having a problem with master pages.
    I have a small site with about 15 pages, each of the pages have different <body onload="" functions and I've found with using masterpages I cannot have different <html> tags in different pages.
    And as a "duh", you can't really include all the onloads on the masterpage lol, or that nice little yellow icon will forever popup on IE's taskbar.... Any advice?
    Actually, any advice other than make different masterpages for each would be best lol

  • Re: Use masters or not?

    11-01-2006, 7:33 PM
    Answer
    • Member
      581 point Member
    • surfer5
    • Member since 10-30-2006, 9:00 PM
    • Tallahassee, FL [USA]
    • Posts 108

    You can definitely use master pages to accomplish this (at least if I understand what you are asking).

    What you would want to do is pass a variable you are setting in the content page to the master.  For example, you create a string variable in your master page to hold the javascript code you want to include in your body onLoad() event.  Each content page populates that string variable in order to create different onLoad() event functions for each page.  That way you can use one Master Page but have that particular functionality be unique for each content page.  Does that make sense?

    To do this, you have to do a little setting up.  First, here is how I have the code behind on my Master Page:

    public partial class MasterPage : System.Web.UI.MasterPage
    {
        protected string onLoadEvent = "";
    
        public string myVariable
        {
            get { return ""; }
            set { onLoadEvent = value; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }

     We are creating a variable, onLoadEvent, that we will use for the onLoad() event of the Master page.  We have set up a public variable, myVariable, to capture values from the content page that is then passed on to onLoadEvent.  In the body tag of the master page itself, I have it set up like this:

    <body onload="<%= onLoadEvent %>">

    This puts the value of the onLoadEvent variable we set up in the code behind in the onLoad() event of the body tag.

     In the content page, we need to add a new MasterType directive just under the Page directive.  Basically, my content page looks like this:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Untitled Page" %>
    <%@ MasterType VirtualPath="~/MasterPage.master" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    </asp:Content>

     Finally, I need to set the value of myVariable in the Page_Load() event of the content page.  I can do that like this:

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.myVariable = "document.write('hello, world!')";
        }
    }

     Obviously you would want to change the javascript code; I just used something to test out with.  But you could change the variable myVariable on the master page from each of the content pages that inherits it.

    This should solve your problem.

  • Re: Use masters or not?

    11-01-2006, 8:55 PM
    • All-Star
      23,606 point All-Star
    • azamsharp
    • Member since 06-11-2003, 9:36 AM
    • Houston,Texas
    • Posts 4,519
    • TrustedFriends-MVPs

    Check out this post:

    http://geekswithblogs.net/azamsharp/archive/2006/04/25/76390.aspx

     

    HighOnCoding
    Get high on ASP.NET articles, videos, podcasts and more!
  • Re: Use masters or not?

    11-01-2006, 10:26 PM

    Wow, thanks for the response, and the time it probably took as well!

    I'm going to give that a shot, I think that'll be the best solution, it makes sense looking at it actually.
    Sure Beats my idea of writing a "response" class with every single function inside of it (does that make sense to you? lol) click -> to class -> switch -> back to class -> write >>forty functions later you have an ONLOAD and a user who never knew how much work it took lol ;)
    You people who use this forum sure are quick to respond and seem to enjoy giving very well explained answers,

    Thanks so much,

    Rob Sitter

  • Re: Use masters or not?

    11-01-2006, 10:30 PM

    I will try that and post the result within the next few hours, Oh the FUN, don't you just enjoy learning new things! lol, new ways to mess up my old code lol,

    Thanks again

  • Re: Use masters or not?

    11-01-2006, 10:45 PM

    Thanks the both of you very much, I did both and now everything is great!

    Now, back to work...

Page 1 of 1 (6 items)