I. C # five methods for calling Javascript on the background
1. onclientclick (vs2003 does not support this method)
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "client_click ()" onclick = "button#click"/>
Client_click () is a JavaScript method.
2, button1.attributes. Add ("onclick", "Return client_click ()");
"Client_click ()" is a foreground method that can be replaced with a general script such as retrun confirm ('Are you sure you want to delete it? ')
3. I thinkIs the bestClientscript. 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 ());
4 . use response. write script using the write method
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 the user-defined functions in the script file cannot be called, and only internal functions can be called. The specific user-defined functions can only be called in response. write the function definition in write, such as response. write ("<SCRIPT type = 'text/JavaScript '> function myfun (){...} </SCRIPT> ");
5 Dynamically add scripts using the clientscript class
The usage is as follows: add where you want to call a Javascript script FunctionCodeMake sure that myfun has 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 call the UDF in the script file.
2. Execute functions in C # code in JavaScript Functions:
Method 1:
1 Create a button to write the called or processed content to button_click in the background;
2 write a JS function on the front end with the content of document. getelementbyid ( " btn1 " ). click ();
3 call JS functions on the front end or back end to stimulate click events, access the C # function in the background;
Method 2:
1 , < Script Language = " Javascript " >
<! - -
Function _ dopostback (eventtarget, eventargument)
{
VaR theform = Document. form1; // Indicates the form of runat = server.
Theform. _ eventtarget. Value = Eventtarget;
Thefrom. _ eventargument. Value = Eventargument;
Theform. Submit ();
}
-->
</ Script >
< Input ID = " Button1 " Type = " Button " Name = " Button1 " Value = " Button " Onclick = " Javascript :__ dopostback ('button1 ','') " >
Method 3: Use the _ dopostback method to directly execute the background method of the server-side control.
Function ctrlsave ()
{
If (event. ctrlkey & event. keycode = 83)
_ Dopostback ('btnsave ','');
}
Or
Eval (_ dopostback ('btnsave', '') only executes the background method of the control on the service end, and skips the onclientclick method on the front end.
However, use document. getelementbyid ("Btnsave"). Click (); the control js method is executed.