javascript in page load

G

Guest

hey all,

is it ok or normal to use ClientScript.RegisterStartupScript in the c#
code-behind page load event? and do i absolutely need to check for
ClientScript.IsStartupScriptRegistered("key"), especially in the page load?

thanks,
rodchar
 
M

Mark Rae [MVP]

is it ok or normal to use ClientScript.RegisterStartupScript in the c#
code-behind page load event?

Sure - what reservations do you have about this...?
and do i absolutely need to check for
ClientScript.IsStartupScriptRegistered("key"),
especially in the page load?

Only if your page would allow the script to be registered more than once,
e.g., via UserControls...
 
G

Guest

here's my javascript that's in the page load so far:

ClientScript.RegisterStartupScript(this.GetType(), "resetOnlyDiv",
"y=document.getElementById('OnlyDIV');" +
"c=document.getElementById('scrvalue');" +
"y.scrollLeft=c.value;" +
"var btnLeft=document.getElementById('BtnLeft');" +
"btnLeft.disabled=y.scrollLeft==0;", true);

i'm just wondering if this looks like stuff i can just put in the
<head><script> section?
 
M

Mark Rae [MVP]

i'm just wondering if this looks like stuff i can just put in the
<head><script> section?

I don't see why not... The server-side methods are great for dynamic
JavaScript, of course, but if your script never changes then there's no real
benefit in using them...

I'm a big fan of JavaScript, and never create static JavaScript
server-side...
 
B

bruce barker

RegisterStartupScript ignores secondary calls with the same key (thus
there is no way change the script once you register it). if you had a
lot of code to determine script, you can use IsStartupScriptRegistered
to skip it.

-- bruce (sqlwork.com)
 
G

Guest

i mean will one way run all the time and the other just run once?

do the following 2 ways do the same thing?

<head>
<script type="text/javascript">
var y=document.getElementById('OnlyDIV');
var c=document.getElementById('scrvalue');
....
</script>

-- vs --

protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "resetOnlyDiv",
"var y=document.getElementById('OnlyDIV');" +
"var c=document.getElementById('scrvalue');" +
....
}
 
T

TarTar

Hi Rodchar,

Your script refers to HTML elements that need to exist when the script is
executed. When you register a piece of JavaScript code with
RegisterStartupScript, the code is inserted after creation of these
elements, and that's fine. Inserting the code into the <head><script>
section would not work as the elements you refer to ('OnlyDIV', 'scrvalue',
and 'BtnLeft') are not created at this point.

If you prefer to put the code into the <head><script> section, wrap it into
a function.
For example:
function OnStartup()
{
// your code here
}

and then register just a call the function:

ClientScript.RegisterStartupScript(this.GetType(), "resetOnlyDiv", "<script
language=javascript>OnStartup()</script>");

This way you could modify your JavaScript function even at run-time without
a need for recompiling.

Leszek "TarTar"
 
G

Guest

thank you for that insight, i appreciate it. and also thank you everyone for
all the helpful replies.
rod.
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top