I am trying to dynamically set the Master Page Body OnLoad Event script from a Child Page. I would like to use the ScrollTo function to bring into view (vertically) a panel being displayed under a GridView control. Other posts that I have read mention using
the Body Unload event to make this work. Unfortunately, from the child page, I am unable to set a variable which will complete the following: <BODY onLoad="<%=jsFunction %>" > contained within the Master page. I would send the name of the control on the child
page to the Javascript function to scroll it into view.
I have had no success so far and I was hoping that someone could help.
You can try to add the runat=server for the body tags, and then expose the body as a public property in the master page codebehind. So you can acess the body tag in the content page and set the body's onload event dynamically.
1. The add the runat=server for the body tag:
<body id="myBody" runat="server">
2. Expose the body in the master page's codebehind:
public HtmlGenericControl body
{
get
{
return this.myBody;
}
}
3. Add the @MasterType for the content page:
<%@ Page Language="C#" MasterPageFile="~/menu/master/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="menu_master_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/menu/master/MasterPage.master" %>
4. add the onload event for the body in the content page codebehind:
wwacker
Member
3 Points
6 Posts
Dynamically Setting Master Page Body OnLoad Event script from a Child Page
Aug 25, 2008 07:48 PM|LINK
I am trying to dynamically set the Master Page Body OnLoad Event script from a Child Page. I would like to use the ScrollTo function to bring into view (vertically) a panel being displayed under a GridView control. Other posts that I have read mention using the Body Unload event to make this work. Unfortunately, from the child page, I am unable to set a variable which will complete the following: <BODY onLoad="<%=jsFunction %>" > contained within the Master page. I would send the name of the control on the child page to the Javascript function to scroll it into view.
I have had no success so far and I was hoping that someone could help.
Thanks.
MASTER events child Body OnLoad
Amanda Wang...
All-Star
30008 Points
3104 Posts
Re: Dynamically Setting Master Page Body OnLoad Event script from a Child Page
Aug 27, 2008 06:06 AM|LINK
Hi,
You can try to add the runat=server for the body tags, and then expose the body as a public property in the master page codebehind. So you can acess the body tag in the content page and set the body's onload event dynamically.
1. The add the runat=server for the body tag:
<body id="myBody" runat="server">
2. Expose the body in the master page's codebehind:
public HtmlGenericControl body
{
get
{
return this.myBody;
}
}
3. Add the @MasterType for the content page:
<%@ Page Language="C#" MasterPageFile="~/menu/master/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="menu_master_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/menu/master/MasterPage.master" %>
4. add the onload event for the body in the content page codebehind:
HtmlGenericControl mybody = this.Master.body;
mybody.Attributes.Add("onload", "loadpage()");
mybody.Attributes.Add("onunload","unloadpage()");
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
wwacker
Member
3 Points
6 Posts
Re: Dynamically Setting Master Page Body OnLoad Event script from a Child Page
Aug 27, 2008 04:05 PM|LINK
Worked like a charm. Thanks.
hudibm
Member
99 Points
42 Posts
Re: Dynamically Setting Master Page Body OnLoad Event script from a Child Page
Oct 30, 2008 01:44 PM|LINK
Hi, i followed the steps but it doesnt work and when i debugged this is the error i got
"Cannot get inner content of myBody because the contents are not literal."
any idea waht might be the problem?
GTL
Member
10 Points
7 Posts
Re: Dynamically Setting Master Page Body OnLoad Event script from a Child Page
Nov 18, 2008 12:46 PM|LINK
Perfect. Thanks!
ziggyrafiq
Member
22 Points
6 Posts
Re: Dynamically Setting Master Page Body OnLoad Event script from a Child Page
May 03, 2009 10:31 PM|LINK
try adding the following tag in your content page
<%
@ MasterType VirtualPath="~/MyMenu/Menu1.master" %>Microsoft.net Web Developer/Ms Office SharePoint Server 2007 Administrator & Developer
T:+44 7960442329
E:zr@zaheer-rafiq.com
W:www.zaheer-rafiq.com
Bader
Member
5 Points
38 Posts
Re: Dynamically Setting Master Page Body OnLoad Event script from a Child Page
Aug 23, 2011 07:29 AM|LINK
Hello,
Your solution is good in case there is only one master page.
Please, I need your help in order to solve the same problem when there is a hierarchy of masterpages:
BaseMasterPage (Has the "body" tag) => DefaultMasterPage => Content Page.
It is very appreciated to guide me how to solve the above problem.
Regards,
Bader