JavaScript in content pages?



I used to do the following to set the cursor when there was no masterpage in
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=""
<script language="javascript">
function setcursor()
// -->
<body onload="setcursor().......

But now with a masterpage there is no body and I can find no place to put my
onload="setcursor". Also where does the <script go now? It doesn't like it
anyplace I try.



1. to set focus to a control in ASP 2.0 you do not need to write your own
javascript - the framework provides a (Page) method for that -"SetFocus".

2. If you need to include javascript, you can use various methods of
Page.ClientScript object, such as RegisterStartupScript,
RegisterClientScriptBlock, RegisterClientScriptInclude, etc

2. If you want to include javascript into your <head>, you could include a
literal in your page Header (either declaratively in Master page, or
programmatically in your page), and then set your literal text to the script
you need, similar to the following:
Literal ltr = new Literal;
ltr.Text = "<script type='text/javascript'>alert('Hello world');</script>";

3. You can also declaratively add javascript to your content pages:

<asp:Content ID="cnt" ContentPlaceHolderID="plh" Runat="Server">
<script type="text/javascript" src="scripts/myLinkedScript.js"></script>
<script type="text/javascript">
alert('Another hello');
// -->

Steven Cheng[MSFT]

Hi Gary,

From your description, you're wondering how to register some client script
to manipulate some html input elements on a master-page based ASP.NET web
page, correct?

For the original script you provided:

<script language="javascript">
function setcursor()
// -->

the limitation here (for master page based one) is that the "tbEmail" is
hard coded, you can change it to a more flexible one. e.g.
function setcursor(elmId)

this function can be defined in master page(or register programmatically in
content page).

And in the content page (that applies the master page), you can use code to
register some script block or startup script .e.g
string script = @"setcursor('" + TextBox1.ClientID + "');";
"setcursor_txt", script, true);

#Page.ClientScript Property

#ClientScriptManager.RegisterStartupScript Method

If you have any other ideas or thought on this, please feel free to post


Steven Cheng

Microsoft MSDN Online Support Lead


Get notification to my posts through email? Please refer to

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at


This posting is provided "AS IS" with no warranties, and confers no rights.


All the above is great but it does not answer Gary's question about how to inject some stuff into the pages body tag when using masterpages.

George Ter-Saakov

why do you want that?

if for body.onload event simply has it anywere on a page.


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