1. Using the Response.Write method
The code is as follows:
Response.Write ("<script type= ' Text/javascript ' >alert (" XXX ");</script>");
The flaw in this method is that you cannot invoke a custom function in the script file, only the intrinsic function can be called, and the custom function can only be written on the Response.Write function definition, such as
Response.Write ("<script type= ' Text/javascript ' >function myfun () {}</script>");
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.
2. Using the ClientScript class
The code looks like this: add code where you want to invoke a JavaScript script function, and be sure that myfun is already 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.
There are 3 overloads of the Clientscript.registerstartupscript method:
1:registerstartupscript (Page, String, string) registers a startup script block with the ScriptManager control and adds the script block to the page. You need to add a <script></script> script tag to the third parameter. The first parameter is page pages. The first name of the second script function, randomly. The third one is the script content in the <script></script> form.
2:registerstartupscript (Control, Type, String, String, Boolean) uses the ScriptManager control to register a startup script block for a control in UpdatePanel. and add the script block to the page. The first parameter is the control ID of the script you want to register, and try it as long as it is on this page. The second parameter is the registration script control type, either the GetType () of the control or this, and typeOf (string) is fine. The name of the third script function, casually. The fourth one is the script content. The fifth one is to indicate whether to add the script tag again, if the fourth parameter contains the <script></script> tag, false here, otherwise true.
3:registerstartupscript (Page, Type, String, String, Boolean) registers a startup script block for each asynchronous postback to the ScriptManager control and adds the script block to the page. This is needed for the page if it is useful to Ajax. The first parameter is page pages. The second parameter is the registration script control type, either the GetType () of the control or this, and typeOf (string) is fine. The name of the third script function, casually. The fourth one is the script content. The fifth one is to indicate whether to add the script tag again, if the fourth parameter contains the <script></script> tag, false here, otherwise true.
If the page does not need to run a ajax,cs in the JS code mode can be:
Page.ClientScript.RegisterStartupScript (Page.gettype (), "", "<script>window.open (' default2.aspx ') </ Script> ");
If Ajax is used on the page, the above code will be ineffective even if it is executed. In this case we usually use:
Scriptmanager.registerstartupscript (this. Button1, this. GetType (), "Alertscript", "window.open (' default2.aspx ');", true);
The first parameter is the control ID of the script you want to register, try it, as long as this page is on the line.
The second parameter is the registration script control type, either the GetType () of the control or this, and typeOf (string) is fine.
The name of the third script function, randomly.
The fourth one is the script content.
The fifth one is to indicate whether to add the script tag again, if the fourth parameter contains the <script></script> tag, false here, otherwise true.
If the name of the two script functions registered is the same, the second script function will not execute. Because the script manager uses this string to parse whether it is a script that has already been loaded, it will not be rendered again if it is loaded. So the second one will not load execution.
Ways to call JavaScript in ASP.