asp.net Background registration js four ways to share

  • 2020-11-20 06:03:29
  • OfStack

1. Use Response.Write method

The code is as follows:

 
Response.Write("<script type='text/javascript'>alert("keleyi.com");</script>");  

The drawback of this method is that you cannot call custom functions in the script file, only internal functions can be called, and the custom functions can only be defined in Response.Write.
Such as

Response.Write("<script type='text/javascript'>function myfun(){}</script>");

2. Use ClientScript class

Here's the code: Add the code where you want to call an javascript script function, making sure that MyFun is already defined in the script file.


ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>MyFun();</script>");

This method is a little more convenient than Response.Write. You can call custom functions in the script file directly.


3. Add the Attributes property of the control

Button1. Attributes. Add("onclick","MyFun();" );

It can only be added in Onload or during initialization like onload. The script function is executed first, and the execution order cannot be changed.

Note that in all of the above methods, the background code cannot have the code to convert the current page, such as Redirect, etc., so put the page-turning code in the script

4. Background load data as js parameter to call js method num is the method registration name, each call once, the registration name cannot be the same


      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);

            }


Related articles: