Solved

ASP.Net body onload event with master pages

Posted on 2008-06-17
3
6,080 Views
Last Modified: 2011-09-20
Hi

Am using asp.net 3.5 with master pages.

I need to run some javascript to preload some images in the body onload event but I don't need to run this on every page.

The only body tag is in the master page (mp.master) for the site so currently the event runs for all content pages that whose masterpagefile is mp.master.

How can I run js code in the onload event of some pages only.

Can anyone help?

Wing
0
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Elvio Lujan
ID: 21812475
you can set runat="server" in the body tag into the maseter page... an set the onload event by each page...

<body runat="server" id="bodyMaster">

and for each page where you want to set the body onload event...

Master.FindControl("bodyMaster").Attributes.Add("onload", "someJavavScript");
0
 
LVL 1

Author Comment

by:WingYip
ID: 21830930
Have just picked this up again.
I am trying this but no joy as yet.  For a start, attributes is not a member of the control class so it doesn't compile.  The following compiles but doesnt find the body element.  

Dim cBody As HtmlGenericControl = Me.Page.Master.FindControl("bodyRpMaster")
        cBody.Attributes.Add("onload", "some_jscript")

have set the body in the master to "runat=server" and id to "bodyRpMaster"

I have put all this in the page load event.

Am wondering what to do next
0
 
LVL 1

Accepted Solution

by:
WingYip earned 0 total points
ID: 21946865
This is the way to do it -

        Dim _body As New HtmlControls.HtmlGenericControl
        _body.TagName = "body"
        _body.Attributes.Add("onload", "Javascript code here;")
        Me.Page.Master.Controls.Add(_body)

Wing
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question