asp.net background Call JS 2 methods _ Practical Skills

Source: Internet
Author: User
Tags function definition

1. Using the Response.Write method

The code is as follows:

Copy Code code as follows:

Response.Write ("<script type= ' Text/javascript ' >alert" ("XXX");</script> ");

The flaw in this approach is that it is not possible to invoke a custom function in a script file, only internal functions can be invoked, and a specific call to a custom function can only write a function definition in Response.Write, such as
Copy Code code as follows:

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

2. Use ClientScript class

The code is as follows: Add code where you want to invoke a JavaScript script function, and be aware that Myfun has already been defined in the script file.

Copy Code code as follows:

Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script>myfun ();</script>");

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

3. Attributes properties of the normal add control

For the ordinary button is: Button1.Attributes.Add ("onclick", "myfun ();");

Can only be added in the onload or in an initialization process similar to onload. and the script function is executed first, and the order of execution cannot be changed.

Note that all of the above methods, the background code can not be converted to the current page code, such as redirect, and so on, to put the transfer page code in the script

Related Article

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.