Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i need call two javascript function by use Page.ClientScript.

but when i use
Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", "onload()", true);

i can't use this agin in same page

example this code


Page.ClientScript.RegisterStartupScript(this.GetType(), " onload", "pro('150')", true);
for (int i = 1 ; i<=4;i++)
Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", "onload()", true);

-------------------------------------
just first Page.ClientScript execut
but second no :(
Posted
Updated 18-Jun-11 1:10am
v2

The second parameter in RegisterStartupScript is KEY. So if the script is already added to the page with that key, it will not add it again.

So change the key If you want to execute the script again.

Using IsStartupScriptRegistered you can check if that key already registered on the page

MIDL
Page.ClientScript.RegisterStartupScript(this.GetType(), "pro", "pro('150')", true);
for (int i = 1 ; i<=4;i++)
Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", "onload()", true);

ipt(this.GetType(), "onload", "onload()", true);
 
Share this answer
 
Comments
Ed Nutting 18-Jun-11 7:15am    
Would that work? I would have thought the key also specifies the event - e.g. onload. With your code it may result in an invalid event 'pro'. Please see my answer, which I think is also better to implement as it avoids confusion.
nit_singh 18-Jun-11 7:18am    
No, pro (which is highlighted in BOLD) is only a key, the function pro('150') is registered with that key
Ed Nutting 18-Jun-11 7:19am    
Okay thanks for clearing that up :) Personally I never have found a use for the RegisterScriptMethods - I prefer to just include script tags directly, seems simpler.
Well of course - what you've done is try to overwrite the old registered method. RegisterStartupScript registers code in the onload method to run, the third parameter specifies this code. It does not add on lines of code the to event. Instead you need to register it all at once. Do something like:

C#
string OnLoadScript = "onload(); ";
//Remember to put end line characters after statements or the resulting JavaScript code would be invalid e.g. 'onload()pro('150')' instead of valid: 'onload(); pro('150');'
OnLoadScript += "pro('150'); "
Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", OnLoadScript, true);


Hope this helps,

Ed :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900