Welcome to WebmasterWorld Guest from 54.90.204.233

Forum Moderators: ocean10000

Message Too Old, No Replies

Adding onload to body tag in master page

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


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,
12:48 am on Aug 4, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


Are we talking about on the Browser side or Server side?
1:27 am on Aug 4, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Sorry I should have been more specific. I need to do it in the page load event in the code behind.
2:19 am on Aug 4, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 13, 2005
posts:1078
votes: 0


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-

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


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

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

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


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>

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


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();")
9:04 pm on Aug 4, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


//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'));
}