C # Call the frontend js method in the background

Source: Internet
Author: User

Three methods

1.UseResponse. WriteMethod write script

For example, after you click the button, you must first operate the database. After the operation is complete, it is displayed that the operation is complete. You can write it in the last place you want to call.
Response. Write ("<SCRIPT type = 'text/JavaScript '> alert (); </SCRIPT> ");

This method has a defect that it cannot call the User-Defined Functions in the script file and can only call internal functions.Response. WriteWrite the function definition, for exampleResponse. Write ("<SCRIPT type = 'text/JavaScript '> function myfun () {...} </SCRIPT> ");

2.UseClientscriptClass dynamic add script

Usage:JavascriptAdd a script FunctionCodeEnsure thatMyfunIt has been defined in the script file.

Clientscript. registerstartupscript (clientscript. GetType (), "myscript", "<SCRIPT> myfun (); </SCRIPT> ");

This method is similarResponse. WriteFor more convenience, you can directly call the User-Defined Functions in the script file.

3.CommonAttributesAttribute

For common buttons:Button1.attributes. Add ("onclick", "myfun ();");

OnlyOnloadOr similarOnload. In addition, the script function is executed first, and the execution sequence cannot be changed.

note that in all the methods above, no background code can be used to convert the current page, such as redirect , put the page-turning code in the script

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.