1. Using the Response.Write method
The code is as follows:
Response.Write ("<script type= ' Text/javascript ' >alert (" keleyi.com ");</script>"); The flaw in this method is that you cannot invoke a custom function in a script file, only call an intrinsic function, and call a custom function to write only the function definition in Response.Write, such as Response.Write ("<script type= ' text/ JavaScript ' >function myfun () {}</script> ");
2. Using the ClientScript class
The code is as follows:
Add code where you want to invoke a JavaScript script function, and be sure 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, you can directly invoke the custom function in the script file.
3. Attributes properties for normal add controls
For the normal button is: Button1.Attributes.Add ("onclick", "myfun ();");
Only valid if added in onload or in the initialization process similar to onload. The script function is executed first, and the execution order cannot be changed.
Note that all of the above methods, the background code can not be converted to the current page of the code, such as redirect, to put the paging code in the script
4. Background load data as JS parameter call JS Method num is a method registration name, each call, the registration name cannot be the same as 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);
}
Four ways to register JS with ASP