Master Pages, Javascript, and Body tag

G

Guest

My goal is to populate the body element so that I can add an onkeydown event.

<body onkeydown='x();'></body>

I am currently using master/content pages. I do not want this function on
every page using my master page. I want to be able to decide what pages get
this function on a page by page basis. Because of this I cannot just add the
text to the html side of the master page as it would cause unwanted results
on the pages where I do not want this function to fire. I do not care how
this is implemented as long as it works. It could be from the content page
calling a function in the master page or the individual content pages each
running the 1 or 2 lines of code to make this work. I just cannot get
anything to work.

Does anyone have any ideas? I have not been able to find anything.

Thanks is advance.

PCK
 
J

John Timney \(MVP\)

In the master page where your body tag sits, make the body a control by
giving the runat="server" attribute and a name.

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

In the Page_Load event of the container page

HtmlGenericControl mybody = (HtmlGenericControl)
Page.Master.FindControl("BodyTag");
mybody.Attributes.Add("onkeydown", "alert('hello world')");


--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
 

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

No members online now.

Forum statistics

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

Latest Threads

Top