C # Background execution JavaScript

Source: Internet
Author: User

Method One:

Page.registerclientscriptblock method

Name space: System.Web.UI

This method is now obsolete. Use the Clientscriptmanager.registerclientscriptblock method instead

Example: Page.registerstartupscript ("GGG", "");

Method Two:

<asp:literal id= "Ltscript" runat= "Server" ></asp:Literal>

Then use the literal class when the background code is used

Private void Button1_Click (object  Sender,system.eventargs e) {       "<script type=/" text/javascript/"> The JS function you want to perform;</script>";}

Method Three:

is one of the most flexible, clientscript.registerstartupscript
Example: StringBuilder sb = new StringBuilder ();
Sb. Append ("<script language= ' JavaScript >");
Sb. Append ("Button2_onclick ('" + Serverpath + "')");
Sb. Append ("</script>");
Clientscript.registerstartupscript (this. GetType (), "Loadpicscript", sb. ToString ());

Method Four:

Writing scripts using the Response.Write method

For example, after you click the button, the database is finished, the display is finished, you can write in the last place to call
Response.Write ("<script type= ' Text/javascript ' >alert ();</script>");

One drawback of this approach is that you cannot invoke a custom function in a script file, only call an intrinsic function, and call a custom function to write function definitions only in Response.Write, such as Response.Write ("<script type= ' text/ JavaScript ' >function myfun () {...} </script> ");

Method Five:

Dynamically adding scripts with the ClientScript class

Use the following: Add code where you want to invoke a JavaScript script function, and be sure that Myfun has already been defined in the script file.

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

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

View Original

C # Background execution JavaScript

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.