ASP.NET, C # background javascript to call the front desk of the five methods

Five ways to call the front desk javascript C # background is

because the project needs to use other components of the project team with development of the VC, in the background web code can not access this component, so I had to call the front desk javascript through the background, so that the operation of this component. The Internet to find a look and found that there are three ways to access the code to the foreground:

first, OnClientClick (vs2003 does not support this method)
< ASP: the Button ID = "Button1" runat = "Server" Text = "the Button"  the OnClientClick = " client_click () " the OnClick = " the Button1_Click "/>
client_click () is a method of javascript.

The second, Button1.Attributes.Add ( "onclick", "return Client_Click ()");
"Client_Click ()" is a foreground process can be replaced with a general script such as: retrun confirm ( 'sure to delete it?')

third, I think the most flexible one, ClientScript.RegisterStartupScript
example: the StringBuilder new new SB = the StringBuilder ();
        SB.= Language 'JavaScript'> ");
        sb.append (" Button2_onclick ( ' "+ ServerPath +"') ");
        sb.append (" </ Script> ");
        ClientScript.RegisterStartupScript (this.GetType ()," LoadPicScript ", sb.ToString ());

. the fourth script written by Response.Write method

, such as when you click the button, the first operation of the database, finished after it has finished, you can write in the last place you want to call on the
Response .write ( " < script type = 'text / JavaScript'> Alert (); </ script>");

this method has a drawback is that the function can not be called custom script file, only internal function call, call specific custom functions can only be written in the Response.Write function definitions, such Response.Write ( " < Script type = 'text / JavaScript'>function myfun(){...}</script>");

The fifth dynamically added script ClientScript class

    usage is as follows: Add code in the script where you want to call a javascript function, pay attention to ensure MyFun already defined in the script file.

    ClientScript.RegisterStartupScript (ClientScript.GetType (), "myscript", " < Script> MyFun (); </ Script>");

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

Guess you like

Origin www.cnblogs.com/bdqczhl/p/11124573.html