Asp. NET background invoke three common methods of the foreground JS function

Source: Internet
Author: User

First: Call using the Attributes property in the normal add control

For example, normal buttons like the general: Button1.Attributes.Add ("onclick", "myfun ();");

This method is only valid if it is added in onload or in the initialization process similar to onload. The script function is executed first, and the execution order cannot be changed.


Second: Call using the Response.Write method

For example, as we will often use to the Response.Write ("<scripttype= ' Text/javascript ' >alert (" XXX ");</script>");

However, there is a flaw in this approach, that is, you cannot invoke a custom function in a script file, only the intrinsic function can be called.

A specific call to a custom function can only be written on the Response.Write function definition, such as

Response.Write ("<script type= ' Text/javascript ' >functionmyfun () {}</script>");


The third type: using the ClientScript class

For example, to invoke the Myfun (parameter) function in a JS file, be sure that Myfun has already been defined in the script file.

can be used Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script>myfun (' parameters '); </script > ") method.

This method is more convenient than Response.Write, and you can invoke the custom function directly in the script file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.