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.