简体   繁体   中英

ASP.NET Calling defined JS-function with RegisterStartupScript

In ASP.NET we are calling defined js-functions with the:

Page.ClientScript.RegisterStartupScript(GetType(), "", "JSFuncNameHere();", true);

I wonder:

  • Why there isn't any method, which has a name like: Page.ClientScript.CallJSScript("someJSFunc");
  • Why does the upper-method require the reflection method GetType() ? Something isn't defined at runtime, is it?
  • Why do I need the 2nd argument key ? As I have tested, I can left it empty and the existed JS-function shall be called.
  • Why there isn't any method, which has a name like: Page.ClientScript.CallJSScript("someJSFunc");

Probably because this is more generic solution, since by just adding 2 characters you get the same result and if you need you can add arguments and anything else.

  • Why does the upper-method require the reflection method GetType() ? Something isn't defined at runtime, is it?
  • Why do I need the 2nd argument key? As I have tested, I can left it empty and the existed JS-function shall be called.

For both of these the same reason - the method will detect if you run the same script multiple times and in such case, call it just once. The two arguments are the means how it identifies duplicates - a key is not sufficient since another class in a different library might be using the same key - so you need to pass in the type of your own class to ensure that the script is executed when you want it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM