Using js file in asp.net 2.0

F

Frank

Hi,

I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.

I don't want to use the
Page.ClientScript.RegisterStartupScript(this.GetType(),"displaymessage",
strScript, false);

method because this seemingly requires me to code the JScript on all the
pages (code behind) where I want to run the function, which is stupid.



I think what I want to use is something like :



Designate the body tag as :

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

and in the code behind for a particular page:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "displaymessage();";


HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}



but this give a JS error where it says 'Object Required' where the body tag
is in the rendered html



It does work if I do:

protected void Page_Load(object sender, EventArgs e)

{

try

{

string temp = "alert('Hello World!');";

HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}

catch (NullReferenceException x)

{

Response.Write(x);

}

}



So what is wrong?
 
M

Michael Nemtsev

Hello Frank,

Have u tried to use RegisterClientScriptInclude ?

F> I simply wish to code a JS function ONCE, in a .js file, include the
F> .js file in the Master page, and then in several but not all pages, I
F> wish to execute the function on pageload.
F>
F> I don't want to use the
F> Page.ClientScript.RegisterStartupScript(this.GetType(),"displaymessag
F> e", strScript, false);
F>
F> method because this seemingly requires me to code the JScript on all
F> the pages (code behind) where I want to run the function, which is
F> stupid.

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
F

Frank

Thanks for your repsonse. I have tried this:

Created a file name global.js which contains:
// JScript File

function displaymessage()

{

alert("Hello World!")

}

Then in the Master page I did like so:

<head runat="server">



<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>

</head>

<body runat="server" id="body"



Then in one of the pages in the site I did like so:

protected void Page_Load(object sender, EventArgs e)

{

// Define the name, type and url of the client script on the page.

String csname = "PageLoadScript";

String csurl = "~/common/gjs/global.js";

Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.

ClientScriptManager cs = Page.ClientScript;

// Check to see if the include script exists already.

if (!cs.IsClientScriptIncludeRegistered(cstype, csname))

{

cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));

}

string temp = "displaymessage()";

HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("Body");

body.Attributes.Add("onload", temp);

}



The page starts up but a JS error occurs (Object Expected) and point to the
body tag line... The rendered HTML starts like so:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Untitled Page
</title><link href="../common/css/admin_style.css" rel="stylesheet"
type="text/css" />
<script language="JavaScript" src="common/js/global.js"
type="text/JavaScript"></script>
</head>
<body id="ctl00_body" onload="displaymessage()">
<form name="aspnetForm" method="post" action="test.aspx"
id="aspnetForm">
<div>.

..

..
 
F

Frank

OK, I got it:

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


public void Page_Load(Object sender, EventArgs e)
{
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScript";
String csurl = "~/script_include.js";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the include script exists already.
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname,
ResolveClientUrl(csurl));
}

//for the body onload functionality
string temp = "displaymessage()";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);

}


<html >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClick()"/>
</div>
</form>
</body>
</html>

This example requires a JavaScript file named Script_include.js with the
following contents:


function DoClick() {Form1.Message.value='Text from include script.'}


function displaymessage()
{
alert('Hello World!');
}


*** add <body runat="server" id="body"> to masterpage for the pageload
functionality
 

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