JavaScript in content pages?

G

GaryDean

I used to do the following to set the cursor when there was no masterpage in
use....
<HEAD>
<title>Login</title>
<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="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
<!--
function setcursor()
{
document.Form1.tbEmail.focus()
}
// -->
</script>
</HEAD>
<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.

thanks,
Gary
 
G

Guest

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>";
this.Page.Header.Add(ltr);

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');
// -->
</script>
</asp:Content>
 
S

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()
{
document.Form1.tbEmail.focus()
}
// -->
</script>
=================

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)
{
document.getElementById(elm).focus();
}
<<<<<<<<<<<

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 + "');";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"setcursor_txt", script, true);
}
<<<<<<<<<<<<<<<<

#Page.ClientScript Property
http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript.asp
x

#ClientScriptManager.RegisterStartupScript Method
http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.r
egisterstartupscript.aspx

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

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



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
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


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

Gigabyteconceptman

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.
 
G

George Ter-Saakov

why do you want that?

if for body.onload event simply has it anywere on a page.
<script>
window.onload=myfunction;
</script>

George.
 

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

Top