Solved

ASP.Net body onload event with master pages

Posted on 2008-06-17
3
6,078 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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