0

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.

1 Answer 1

1
  • 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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's worth pointing out that the MSDN documentation for RegisterStartupScript - msdn.microsoft.com/en-AU/library/z9h4dk8y.aspx - provides the answers for points 2 and 3 in "Remarks". When trying to determine the purpose of method parameters, MSDN should always be your first stop :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.