PDA

View Full Version : OnLoad in Content Page


nptalcott
08-06-2008, 07:42 PM
Hi,

Currently I am working on a menu in a SharePoint site where we are using code to create dropdown menus in some content pages. I did not design this site and the original designer seemed to be happy with anything as long as "it works". This is causing some maintainability problems.

Originally we had the menu working by calling the P7_ExpMenu() function using <body onload=""> in the master page while the definition <script type="text/javascript" language="JavaScript" src="menu.js"><script> was located in the content page. This was causing an error on any content page that didn't use the menu as it didn't have the function definition (but it "worked" because there were no menus on these pages to fail). So, as a temporary fix, I moved the definition into the master page header and everything runs properly with no errors.

The problem is:
In the long term I will have no control over the master page being used so the solution must be confined to the content page. This means moving the definition and onload call from the master to the content page. I've tried doing this using window.onload instead of <body onload=""> as a content page does not have a body tag. My code currently looks like:

.....
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

<script type="text/javascript" language="JavaScript" src="menu.js">window.onload=P7_ExpMenu();</script>
.....

This code does not work. Any ideas how I might get this to work without putting code in the master? Is it a problem with where I am invoking the onload call (in PlaceHolderMain)? Is it a problem with the syntax of the call?

I saw another solution where they modify the Page_Load() code behind (.cs) rather than using JavaScript to modify the body tag using the command body.attributes.add("onload","P7_ExpMenu") but I don't know how to do this in SharePoint. Could I just created a .cs file with only the Page_Load() function and put it in the same directory as the content page?

Any help is appreciated,
Nathaniel

nptalcott
08-07-2008, 09:04 PM
I think I’ve found a solution for getting the menus to work. While I cannot access the <body onload=”” ….. > function directly from the content page I can do so by making the body an ASP.Net object in the master page and giving it a name i.e. <body id="body" runat="server" ….. >. We can use a codebehind page for the content page to define an body onload event when the page loads such as:

namespace BusinessName.DepartmentName.ProjectName
{
public class ProjectWebPartPage : System.UI.Web.Page
{
protected void Page_Load(Object sender, EventArgs e)
//Page_Load is called after the entire page is loaded
{
//Inject onload
HtmlGenericControl myBody = (HtmlGenericControl)Master.FindControl("body");
myBody.Attributes.Add("onload", "P7_ExpMenu()");
}
}
}

The problem:
While this would work well on a normal ASP.Net page the page directive <%@ Page Language=”C #” CodeFile=”ProjectWebPartPage.aspx.cs” ….. > is not allowed in SharePoint and loading a content page with this directive gives the error "The codefile attribute on the page directive is not allowed in this page. " There seems to be a workaround for this using the inherits attribute to refer to the namespace rather than using the codefile attribute to refer to the file which can be found here: UsingCodeBehindFilesInSharePointSites.aspx (http://www.andrewconnell.com/blog/articles/UsingCodeBehindFilesInSharePointSites.aspx). I'm still trying to understand this...

nptalcott
08-12-2008, 07:52 PM
Well, I've discovered that I won't be permitted to use any ASP.Net code from a code behind page...

I decided the simplest method was to get rid of the seperate .js file, put both the markup and the javascript into a web part, and upload the webpart to SharePoint. Then I registered the web part in the site template (tagprefix=) and added it to a content area. Now I can update the DropDownMenu web part and it will change the menu on every page...

ziyad6
08-28-2008, 09:05 AM
You can make avail of either of these two methods..check my blog here http://dotnetsoldier.blogspot.com/2006/08/body-onload-event-in-content-pages.html

:thumbsup:

mkel
09-11-2008, 11:03 PM
Try removing the parenthesis () after P7_ExpMenu so Javascript will assign the entire function to the event instead of trying to execute the function and place the results in the event.

<script type="text/javascript" language="JavaScript" src="menu.js">window.onload=P7_ExpMenu();</script>

So the result would be:

<script type="text/javascript" language="JavaScript" src="menu.js">window.onload=P7_ExpMenu;</script>


Let me know if it works.

MK

Xesco
10-01-2010, 12:40 PM
Try removing the parenthesis () after P7_ExpMenu so Javascript will assign the entire function to the event instead of trying to execute the function and place the results in the event.

<script type="text/javascript" language="JavaScript" src="menu.js">window.onload=P7_ExpMenu();</script>

So the result would be:

<script type="text/javascript" language="JavaScript" src="menu.js">window.onload=P7_ExpMenu;</script>


Let me know if it works.

MK

Wow! Two years later, I thank you, works perfect You are a master!