A summary of the five ways in which C # backstage calls foreground JavaScript

Source: Internet
Author: User
Tags add functions
Looking for it on the Internet, there are three ways to access the foreground code:

First, OnClientClick (VS2003 does not support this method)

<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "Client_click ()"/>

Client_click () is a way of JavaScript.

The second type, BUTTON1.ATTRIBUTES.ADD ("onclick", "Return Client_click ()");

"Client_click ()" is a foreground method, can replace the general script such as: Retrun confirm (' OK delete? ')

The third is the one I think is 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 ());

The fourth kind. Writing scripts with the Response.Write method

For example, after you click the button, the first operation of the database, after the display has been completed, you can finally want to call the place to write

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

A flaw in this approach is that you cannot call custom functions in script files, only internal functions, and specific calls to custom functions can only be written in Response.Write, such as Response.Write ("<script type=" text/ JavaScript ' >function myfun () {...} </script> ");

Fifth type dynamically add scripts with ClientScript class

Use the following: To add code where you want to invoke a JavaScript script function, be aware 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, and you can call the custom function in the script file directly.

Can be executed anywhere in the program, O (∩_∩) o ..., is it good?

Note The order of execution: Execute the client first, then execute the server

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.