Forum Moderators: open

Message Too Old, No Replies

Adding onload to body tag in master page

         

andrewsmd

9:27 pm on Aug 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have searched everywhere and I cannot find a code snippet in vb that shows you how to programatically add an onload event to the body tag within a master page. Basically I have one page that I need to add an onload even to the body tag, but only for that page. Since it uses a master page the body tag is in there and I cannot figure out how to add the onload event when that page comes up from the vb code behind. Any ideas? Thanks,

Ocean10000

12:48 am on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are we talking about on the Browser side or Server side?

andrewsmd

1:27 am on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I should have been more specific. I need to do it in the page load event in the code behind.

carguy84

2:19 am on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the master page, create a literal tag:
<asp:Literal id="litBody" runat="server" />
And stick it in where you want the body tag.

Then in the master page code behind create a public property:

Public Property BodyTag() As String
Get
Return 1
End Get
Set(ByVal value As String)
litDescription.Text = value
End Set
End Property

Also set the ClassName,
<%@ master language="VB" ClassName="tmaster" Debug="false" %>

In your ASPX page:
Dim mMaster As ASP.tmaster = CType(Master, ASP.tmaster)
mMaster.BodyTag = "<body onload=""yourFunction()"">"

On your other pages, you'll need to set the body tag as well, or set a default in your Master Page.

Chip-

andrewsmd

1:53 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've done some more research, and to be honest, I don't want to have to set the body tag on every page. One, it would be annoying but two, if someone comes along in a few years and edits this page, that could prove to be a big problem that would difficult to find. I have this code in my page behind but it doesn't append the body tag. Any ideas why
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim myBodyTag As HtmlGenericControl
myBodyTag = Page.Master.FindControl("MasterBody")
myBodyTag.Attributes.Add("onload", "loadEarth('mapDiv');")
myBodyTag.Attributes.Add("onunload", "GUnload();")

End Sub

Ocean10000

5:26 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is another way without needing to do any changes to the master page what so ever. It can be done all client side with a few lines of code.

<script>

function GUnload() {
//code goes here
}
function loadEarth() {
//code goes here
}
window.onload = loadEarth;
window.onunload = GUnload;
</script>

andrewsmd

7:51 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That won't work with the google maps for me for some reason. I got it working this way.
Dim myBodyTag As HtmlGenericControl
myBodyTag = Page.Master.FindControl("MasterBody")
myBodyTag.Attributes.Add("onload", "loadEarth('divMap');")
myBodyTag.Attributes.Add("onunload", "GUnload();")

Ocean10000

9:04 pm on Aug 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



//This should work just as well, even with google maps.
if (window.addEventListener)
{
window.addEventListener('load', loadEarth('divMap'), false);
}
else if (window.attachEvent)
{
window.attachEvent('onload', loadEarth('divMap'));
}