To the project needs, the other project team with VC developed components, in the Web background code can not access this component, so had to call the foreground JavaScript through the background to operate this component. Looking 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"
Nclientclick= "Client_click ()"
nclick= "Button1_Click"/>
Client_click () is a way of JavaScript.
The second type, BUTTON1.ATTRIBUTES.ADD ("onclick", "Return Client_click ()");
"Client_click () is a foreground method that can be replaced with a generic 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.
It can be done anywhere in the program.
Note The order of execution: Execute the client first and then execute the server.