Solved

ASP.Net body onload event with master pages

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

Expert Comment

by:lem2802
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now