Putting same Javascript function in every ASP.net page

S

Shikari Shambu

Hi,
I have an ASP.NET website that has pages that inherit from a BasePage. I
want all pages to have some common Javascript functions.

I tried doing the following

In the Base Page
protected System.Web.UI.HtmlControls.HtmlGenericControl body;

protected void Page_Load(object sender, System.EventArgs e)

{


body.Attributes.Add("onload","javascript:alert('Hello World')");

}

And, when I access the child page MyPage.aspx I get

Object reference not set to an instance of an object.

How do I add same javascript to the body onload tag of all my pages.

TIA
 
J

Juno

Hi,

Did you put runat=server in body tag,like <body id=Body runat=server>
or you may initiate a class before you use it,
HtmlGenericControl body = new HtmlGenericControl("body");
 
M

Mark Fitzpatrick

A lot of the times this happens, it's because you have the control
referenced in a class, but don't have the control defined in the actual ASPX
page. Make sure that on every page that inherits from this that the body tag
has a runat="server" attribute and you have set the id to "body". Otherwise
when it comes time to render it will have no idea where the tag is that it's
supposed to add an attribute to.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top