473,698 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript From PageLoad()?

Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet " type="text/css"
/>

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

</head>

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

Dec 6 '06 #1
8 2604
Frank,

There is an ASP.NET feature to add scripts as you are trying to do with
a generic html control. Look for a method in
Page.ClientScri pt.RegisterClie ntScriptInclude (...).

Below is a code snippet which I use. You can also register a block as
well as other option. Be sure to always check if the script block is
already registered in case the event handler you place this in is
called more than once.

http://msdn2.microsoft.com/en-us/lib...r_methods.aspx
#region " Methods "
private void RegisterScriptI nclude(string key)
{
if
(!Page.ClientSc ript.IsClientSc riptIncludeRegi stered(key))
{
string url =
Page.ClientScri pt.GetWebResour ceUrl(GetType() , Prefix + key);

Page.ClientScri pt.RegisterClie ntScriptInclude (typeof(Embedde dScriptsManager ),
key, url);
}
}
#endregion
You can actually get the full source for the project which includes
this method.

http://svn.offwhite.net/svn/SmallSha...Scripts/trunk/

I create a script manager for common JS libraries like Prototype and
Scriptaculous. It manages those scripts as embedded resources.

Brennan Stehling
http://brennan.offwhite.net/blog/
Frank wrote:
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet " type="text/css"
/>

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js function, it
works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript error
which states it is expecting an object, but I can't figure out what object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!
Dec 7 '06 #2
rb

I don't get it. Why don't you just do it as usual:

<script language="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS( );">

rb
"Frank" <fk******@pfmai l.comwrote in message
news:u7******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet "
type="text/css" />

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js function,
it works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript error
which states it is expecting an object, but I can't figure out what object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!

Dec 7 '06 #3
rb,

This technique generates those script tags. With ASP.NET you can build
a page with a series of User Controls which are fragments you can use
throughout the page. And a User Control does not have a HEAD tag at
all. It is like a Server-Side Include. You may have a User Control
represent a product display and have 6 products shown on the page. If
you had that control use a little Javascript you use this technique to
tell it to load the script just once by making sure it is not
registered before you do. Then your User Control can call methods in
that script files as needed.

For the sites I build with ASP.NET I take advantage of Master Pages
which act like a template for the whole website. The Master Page may
have User Controls for the navigation and footer areas. Then the Page
portion may use more User Controls.

I break things up into small User Controls as much as reasonable to
greatly simplify maintenance.

But if I do have a script I know I always wanted included for every
page I can still just place it in the Master Page header as a regular
script page.

Brennan Stehling
http://brennan.offwhite.net/blog/

rb wrote:
I don't get it. Why don't you just do it as usual:

<script language="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS( );">

rb
"Frank" <fk******@pfmai l.comwrote in message
news:u7******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet "
type="text/css" />

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js function,
it works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript error
which states it is expecting an object, but I can't figure out what object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!
Dec 7 '06 #4
rb

Thanks for the lecture but RegisterClientS criptInclude and UserControls
don't have much to do with Frank's question.

He works with master page and wants to use/call javascript function from
body.onload. He encountered a problem where by if he calls alert() from
onload everything seem to be fine. However, if he calls actual javascript
function he gets an error.

So, back to my "solution", since nothing seem to be dynamic, why not just
hardcode call to javascript from body.onload.

rb
"Brennan Stehling" <of******@gmail .comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.com...
rb,

This technique generates those script tags. With ASP.NET you can build
a page with a series of User Controls which are fragments you can use
throughout the page. And a User Control does not have a HEAD tag at
all. It is like a Server-Side Include. You may have a User Control
represent a product display and have 6 products shown on the page. If
you had that control use a little Javascript you use this technique to
tell it to load the script just once by making sure it is not
registered before you do. Then your User Control can call methods in
that script files as needed.

For the sites I build with ASP.NET I take advantage of Master Pages
which act like a template for the whole website. The Master Page may
have User Controls for the navigation and footer areas. Then the Page
portion may use more User Controls.

I break things up into small User Controls as much as reasonable to
greatly simplify maintenance.

But if I do have a script I know I always wanted included for every
page I can still just place it in the Master Page header as a regular
script page.

Brennan Stehling
http://brennan.offwhite.net/blog/

rb wrote:
>I don't get it. Why don't you just do it as usual:

<script language="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS( );">

rb
"Frank" <fk******@pfmai l.comwrote in message
news:u7******* *******@TK2MSFT NGP02.phx.gbl.. .
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet "
type="text/css" />

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js
function,
it works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript
error
which states it is expecting an object, but I can't figure out what
object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!

Dec 7 '06 #5
Thanks for the responses so far everyone.

rb, the reason I don't just add <body id="body" onload="TestJS( );"to the
masterpage html, is because this is a function I want to execute on only
certain pages. If I out it into the Masterpage html, it will execute on all
pages.

And Brennan, I have seen a lot of code samoles that use the
Page.ClientScri pt.RegisterClie ntScriptInclude (...). you mentioned, but man,
what a pain. If I want to use the function on several different pages, I
themn have to script it in all the code behind pages and then I believe it
embedds the script in the resulting html which is ugly.

I simply wish to code the JS function once in the code behind and then
execute it from some individual pages within a site... there must be a way
to do this. I spent too much time on this already but the code I posted
seems the closest to what I am looking for, however as it is, it throws a JS
error and I am unsure how to resolve it. Thanks for any further effort or
consideration.
Frank
"rb" <rajko_at_nospa m_sheermomentum .comwrote in message
news:eP******** ******@TK2MSFTN GP05.phx.gbl...
>
Thanks for the lecture but RegisterClientS criptInclude and UserControls
don't have much to do with Frank's question.

He works with master page and wants to use/call javascript function from
body.onload. He encountered a problem where by if he calls alert() from
onload everything seem to be fine. However, if he calls actual javascript
function he gets an error.

So, back to my "solution", since nothing seem to be dynamic, why not just
hardcode call to javascript from body.onload.

rb
"Brennan Stehling" <of******@gmail .comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.com...
>rb,

This technique generates those script tags. With ASP.NET you can build
a page with a series of User Controls which are fragments you can use
throughout the page. And a User Control does not have a HEAD tag at
all. It is like a Server-Side Include. You may have a User Control
represent a product display and have 6 products shown on the page. If
you had that control use a little Javascript you use this technique to
tell it to load the script just once by making sure it is not
registered before you do. Then your User Control can call methods in
that script files as needed.

For the sites I build with ASP.NET I take advantage of Master Pages
which act like a template for the whole website. The Master Page may
have User Controls for the navigation and footer areas. Then the Page
portion may use more User Controls.

I break things up into small User Controls as much as reasonable to
greatly simplify maintenance.

But if I do have a script I know I always wanted included for every
page I can still just place it in the Master Page header as a regular
script page.

Brennan Stehling
http://brennan.offwhite.net/blog/

rb wrote:
>>I don't get it. Why don't you just do it as usual:

<script language="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS( );">

rb
"Frank" <fk******@pfmai l.comwrote in message
news:u7****** ********@TK2MSF TNGP02.phx.gbl. ..
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet "
type="text/css" />

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js
function,
it works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCo ntrol)Master.Fi ndControl("Body ");

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCo ntrol)Master.Fi ndControl("Body ");

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript
error
which states it is expecting an object, but I can't figure out what
object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!


Dec 7 '06 #6
Frank,

One of the ClientScript functions is RegisterStartup Script which does
run when the page is loaded.

http://msdn2.microsoft.com/en-us/lib...tupscript.aspx

As for placing all of those code snippets all over the page, what I
like to do is place as much of the Javascript into a .js file and use
the RegisterScriptI nclude function. Then in the places which do need
to call Javascript functions I add them as them as attributes.

Like the HyperLink control... It has an OnClick property but that is
for the server-side event handler. So I do this...

HyperLink1.Attr ibute["onclick"] = "runFunction(); ";

Regardless of how you load the Javascript file you will still need to
inline all of those methods calls in event handers.

I also make use of the commonly used and well-tested Javascript
libraries like Prototype and Scriptaculous. By leveraging them I write
less of my own code. And those libraries have already been developed
to work across multiple browsers and platforms. The problem with using
these libraries is that some require others so the dependencies can be
tough to manage over time. So I created this...

http://brennan.offwhite.net/blog/200...beddedscripts/

And since have renamed it and placed the code here...

http://svn.offwhite.net/trac/SmallSh...beddedScripts/

You could use that or create your own custom version of it to help you
manage your scripts.

Brennan Stehling
http://brennan.offwhite.net/blog/

Frank wrote:
Thanks for the responses so far everyone.

rb, the reason I don't just add <body id="body" onload="TestJS( );"to the
masterpage html, is because this is a function I want to execute on only
certain pages. If I out it into the Masterpage html, it will execute on all
pages.

And Brennan, I have seen a lot of code samoles that use the
Page.ClientScri pt.RegisterClie ntScriptInclude (...). you mentioned, but man,
what a pain. If I want to use the function on several different pages, I
themn have to script it in all the code behind pages and then I believe it
embedds the script in the resulting html which is ugly.

I simply wish to code the JS function once in the code behind and then
execute it from some individual pages within a site... there must be a way
to do this. I spent too much time on this already but the code I posted
seems the closest to what I am looking for, however as it is, it throws a JS
error and I am unsure how to resolve it. Thanks for any further effort or
consideration.
Frank
"rb" <rajko_at_nospa m_sheermomentum .comwrote in message
news:eP******** ******@TK2MSFTN GP05.phx.gbl...

Thanks for the lecture but RegisterClientS criptInclude and UserControls
don't have much to do with Frank's question.

He works with master page and wants to use/call javascript function from
body.onload. He encountered a problem where by if he calls alert() from
onload everything seem to be fine. However, if he calls actual javascript
function he gets an error.

So, back to my "solution", since nothing seem to be dynamic, why not just
hardcode call to javascript from body.onload.

rb
"Brennan Stehling" <of******@gmail .comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.com...
rb,

This technique generates those script tags. With ASP.NET you can build
a page with a series of User Controls which are fragments you can use
throughout the page. And a User Control does not have a HEAD tag at
all. It is like a Server-Side Include. You may have a User Control
represent a product display and have 6 products shown on the page. If
you had that control use a little Javascript you use this technique to
tell it to load the script just once by making sure it is not
registered before you do. Then your User Control can call methods in
that script files as needed.

For the sites I build with ASP.NET I take advantage of Master Pages
which act like a template for the whole website. The Master Page may
have User Controls for the navigation and footer areas. Then the Page
portion may use more User Controls.

I break things up into small User Controls as much as reasonable to
greatly simplify maintenance.

But if I do have a script I know I always wanted included for every
page I can still just place it in the Master Page header as a regular
script page.

Brennan Stehling
http://brennan.offwhite.net/blog/

rb wrote:
I don't get it. Why don't you just do it as usual:

<script language="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS( );">

rb
"Frank" <fk******@pfmai l.comwrote in message
news:u7******* *******@TK2MSFT NGP02.phx.gbl.. .
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet "
type="text/css" />

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js
function,
it works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript
error
which states it is expecting an object, but I can't figure out what
object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!

Dec 7 '06 #7
Frank,

Also, if you do get a problem with the methods not being immediately
available once the page is loaded you can wrapper you call with a
SetTimeout function with a very low timeout value. There is a bug in
some browsers where it does need that very slight delay.

I had this very problem the other day with Firefox while it was working
fine in IE. The timeout delay made it work.

Brennan Stehling
http://brennan.offwhite.net/blog/
Frank wrote:
Thanks for the responses so far everyone.

rb, the reason I don't just add <body id="body" onload="TestJS( );"to the
masterpage html, is because this is a function I want to execute on only
certain pages. If I out it into the Masterpage html, it will execute on all
pages.

And Brennan, I have seen a lot of code samoles that use the
Page.ClientScri pt.RegisterClie ntScriptInclude (...). you mentioned, but man,
what a pain. If I want to use the function on several different pages, I
themn have to script it in all the code behind pages and then I believe it
embedds the script in the resulting html which is ugly.

I simply wish to code the JS function once in the code behind and then
execute it from some individual pages within a site... there must be a way
to do this. I spent too much time on this already but the code I posted
seems the closest to what I am looking for, however as it is, it throws a JS
error and I am unsure how to resolve it. Thanks for any further effort or
consideration.
Frank
"rb" <rajko_at_nospa m_sheermomentum .comwrote in message
news:eP******** ******@TK2MSFTN GP05.phx.gbl...

Thanks for the lecture but RegisterClientS criptInclude and UserControls
don't have much to do with Frank's question.

He works with master page and wants to use/call javascript function from
body.onload. He encountered a problem where by if he calls alert() from
onload everything seem to be fine. However, if he calls actual javascript
function he gets an error.

So, back to my "solution", since nothing seem to be dynamic, why not just
hardcode call to javascript from body.onload.

rb
"Brennan Stehling" <of******@gmail .comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.com...
rb,

This technique generates those script tags. With ASP.NET you can build
a page with a series of User Controls which are fragments you can use
throughout the page. And a User Control does not have a HEAD tag at
all. It is like a Server-Side Include. You may have a User Control
represent a product display and have 6 products shown on the page. If
you had that control use a little Javascript you use this technique to
tell it to load the script just once by making sure it is not
registered before you do. Then your User Control can call methods in
that script files as needed.

For the sites I build with ASP.NET I take advantage of Master Pages
which act like a template for the whole website. The Master Page may
have User Controls for the navigation and footer areas. Then the Page
portion may use more User Controls.

I break things up into small User Controls as much as reasonable to
greatly simplify maintenance.

But if I do have a script I know I always wanted included for every
page I can still just place it in the Master Page header as a regular
script page.

Brennan Stehling
http://brennan.offwhite.net/blog/

rb wrote:
I don't get it. Why don't you just do it as usual:

<script language="JavaS cript" src="common/js/global.js"
type="text/JavaScript"></script>
<snip>
<body id="body" onload="TestJS( );">

rb
"Frank" <fk******@pfmai l.comwrote in message
news:u7******* *******@TK2MSFT NGP02.phx.gbl.. .
Hi,

I am working with VS.NET 2005

Ultimately, I wish to call a JavaScript function from a .js file

In the Master page, I have:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Admin Pages</title>

<link href="common/css/admin_style.css " rel="stylesheet "
type="text/css" />

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

</head>

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

.

.

.

</body>

</html>

In the individual page code behind where I wish to call the js
function,
it works if I use:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "alert('Hel lo World!');";

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

But if I try to call a function from the js file like so:

protected void Page_Load(objec t sender, EventArgs e)

{

try

{

string temp = "TestJS();" ;

HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );

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

}

catch (NullReferenceE xception x)

{

Response.Write( x);

}

}

It fails. I don't get an ASP.NET exception, but rather a JavaScript
error
which states it is expecting an object, but I can't figure out what
object
it means. My js file looks like this:

// JScript File

function TestJS()

{

alert('Hello World!'};

}

The rendered html contains :

<body id="ctl00_body " onload="TestJS( );">

Can anyone give some help please?

Frustrated to no end over what should be a simple operation!!

Dec 7 '06 #8
OK, I got it...Thanks for all your help everyone:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
public void Page_Load(Objec t sender, EventArgs e)
{
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScr ipt";
String csurl = "~/script_include. js";
Type cstype = this.GetType();

// Get a ClientScriptMan ager reference from the Page class.
ClientScriptMan ager cs = Page.ClientScri pt;

// Check to see if the include script exists already.
if (!cs.IsClientSc riptIncludeRegi stered(cstype, csname))
{
cs.RegisterClie ntScriptInclude (cstype, csname,
ResolveClientUr l(csurl));
}

//for the body onload functionality
string temp = "displaymessage ()";
HtmlGenericCont rol body =
(HtmlGenericCon trol)Master.Fin dControl("Body" );
body.Attributes .Add("onload", temp);

}
<html >
<head>
<title>ClientSc riptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClic k()"/>
</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
Dec 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5069
by: cheezebeetle | last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when they press "Cancel" they call another ASPX function. My code now is: System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf) System.Web.HttpContext.Current.Response.Write("if (confirm('Are you sure you want to...
1
1276
by: Tom S | last post by:
I've used C# to register a javascript function in the PageLoad event of a web form page. My problem is that I can't seem to figure out how to call it. I've tried the following, all to no avail: <a href="javascript: myscript()">click here</a> <asp:hyperlink id="hplnk1" runat="server" target="javascript: myscript()">click here</asp:hyperlink> <asp:linkbutton id="lnkbtn1" runat="server" onclick="javascript: myscript()">click...
4
1977
by: Nevyn Twyll | last post by:
I have a web form, with c# code-behind. I have a listbox on the form, bound to a dataset. I want to have 2 buttons/hyperlinks/etc. beside the listbox. When they are clicked, I want to launch a popup form, passing it some information from the listbox (record id) and the linked information. If I could show the data in place of the page showing, I know how to get the data and use Response.Redirect(). Since it needs to be a popup, I know...
4
15936
by: Frank | last post by:
Hi, I am working with VS.NET 2005 Ultimately, I wish to call a JavaScript function from a .js file
6
10292
by: MayBoy | last post by:
Hi There I am trying to use the Goto method of the Word ActiveX object. I am trying to open a document and go to a named bookmark. If I use this code in VB it works, so I'm sure the approach is possible, I just can't get JavaScript to work with it. Here is the code I am using, the error I get from IE is Object Expected: Hope someone can help! Any help would be much appreciated
0
1090
by: sirumalla.srinivas | last post by:
Frens, I am trying to get screen resolution of the client machine using javascript and use those values in my project. I looked at some of the examples given .net groups, i tried to implement the same but everytime i get nulls, i tested javascript by keeping alert statements and everything looks good to me.... I am trying to figure it out what peice of info is missing in this code..... Here is my code
11
3185
by: srini | last post by:
Frens, I am trying to get screen resolution of the client machine using javascript and use those values in my project. I looked at some of the examples given in this group, i tried to implement the same but everytime i get nulls, i tested javascript by keeping alert statements and everything looks good to me.... I am trying to figure it out what peice of info is missing in this code..... Here is my code
5
3429
by: =?Utf-8?B?TWF0Y29u?= | last post by:
Hello, I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is a javascript function (myfunction) defined in the <headof that master page. Is there a way to run the function from the page load event of the page? I am able to run the function when there is a change in, say, a text box, by adding the following to the page load event: TextBox.Attributes.Add("onChange", "javascript:myfunction()")
2
9033
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to figure this problem out. Any suggestions? thanks Joel
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.