1. Using the Response.Write method
The code is as follows:
Copy Code code as follows:
Response.Write ("<script type= ' Text/javascript ' >alert (" keleyi.com ");</script>");
The flaw in this approach is that you cannot invoke custom functions in the script file, only internal functions, and specific calls to custom functions can only be written to the function definition in Response.Write.
Like what
Copy Code code as follows:
Response.Write ("<script type= ' Text/javascript ' >function myfun () {}</script>");
2. Use ClientScript class
The code is as follows: Add code where you want to invoke a JavaScript script function, and be aware that Myfun has already been defined in the script file.
Copy Code code as follows:
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.
3. Attributes properties of the normal add control
For the ordinary button is: Button1.Attributes.Add ("onclick", "myfun ();");
Can only be added in the onload or in an initialization process similar to onload. and the script function is executed first, and the order of execution cannot be changed.
Note that all of the above methods, the background code can not be converted to the current page code, such as redirect, and so on, to put the transfer page code in the script
4. Background load data as JS parameter call JS Method num is the method registration name, each time, the registration name can not be the same
Copy Code code as follows:
int num = 0;
foreach (Roadstation rr in R)
{
num++;
Scriptmanager.registerstartupscript (this,this. GetType (), Num. ToString (), String. Format ("Addtr (' {0} ', ' {1} ');", RR. F_stationname, RR. F_time), true);
}