javascript in page load

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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...
 
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?
 
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...
 
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)
 
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');" +
....
}
 
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"
 
thank you for that insight, i appreciate it. and also thank you everyone for
all the helpful replies.
rod.
 
Back
Top