Solved

ASP.Net body onload event with master pages

Posted on 2008-06-17
3
6,073 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
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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