Summary of common methods for ASP.NET to invoke javascript scripts

  • 2020-05-07 19:28:40
  • OfStack

Call the javascript function directly from the foreground

It's easy to put an script element between the head elements and set the type element to "text/javascript"
Such as:
 
<head runat="server"> 
<script type="text/javascript" > 
function ShowName(str) 
{ 
alert(" Your name is :("+str+")"); 
} 
</script> 
<title>using javascript</title> 
</head> 

Then, between the body elements, access the javascript function by event such as to access the javascript function by the click event of button1 (onclientclick)
Here's an example:
 
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('XXX')" /> 

When you click button, it says, "your name is XXX."
This is a simple javascript function.

2, called from the foreground through the js file

Method is identical to (1) except that the.js file needs to be specified
Here's an example:
 
<head runat="server"> 
<script type="text/javascript" src="JScript.js"> 
</script> 
<title>using javascript</title> 
</head> 

Then, between the body elements, access the javascript function via events such as the click event of button1 (onclientclick)
Here's an example:
// at this point the ShowName method must be in the.js file
< asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('XXX')" / >

3. Call the javascript function in the background. The function is in the.js file

The head element in the foreground
 
<head runat="server"> 
<script type="text/javascript" src="JScript.js"> 
</script> 
<title>using javascript</title> 
</head> 

The following code needs to be added in the background
Button1.Attributes.Add("onclick", "showname1(XXX)");

The javascript function is called in the background. The function is written in the.js file, but is not defined in the foreground

 
// To obtain .js file  
string myscript = "JScript.js"; 
// registered .js file ,  If you look at the source at this point , You get the following code  
//<script> src ="JScript.js" type="text/javascript"><script> 
Page.ClientScript.RegisterClientScriptInclude("myKey", myscript); 
// Same as above  
Button1.Attributes.Add("onclick", "showname1(123)"); 


5. Write the script using the Response.Write method

For example, after you click the button, you work with the database first, and when you're done, it says it's done
Response.Write(" < script type='text/javascript' > alert(); < /script > ");
The drawback of this method is that it cannot call the custom function in the script file, but can only call the internal function. The specific function to call the custom function can only write the function definition in Response.Write, such as Response.Write ("). < script type='text/javascript' > function myfun(){...} < /script > ");

6. Dynamically add script with ClientScript class

The usage is as follows: add code where you want to call some javascript script function, be careful to ensure 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 and can call custom functions directly from the script file.
Note that in all of the above methods, the background code cannot have the code to convert the current page, such as Redirect, etc., to put the page-forwarding code in the script

Related articles: