Many people call client functions on the server, that is, they call the script functions defined in JavaScript scripts in ASP. After research, we found some stubborn methods.
1. Write the script using the response. 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> ");
2. 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.
3. Add the attributes attribute of a common control.
For common buttons: button1.attributes. Add ("onclick", "myfun ();");
It can only be added in onload or in initialization process similar to onload. In addition, the script function is executed first, and the execution sequence cannot be changed.
Note: In all the above methods, the background Code cannot convert the code of the current page, such as redirect. You need to put the page conversion code in the script.