1. Call javascript functions directly in the foreground
It's very simple, add script elements between the head elements and set the type element to "text/javascript"
like:
<head runat="server">
<script type="text/javascript" >
function ShowName(str)
{
alert("Your name is:("+str+")");
}
</script>
<title>using javascript</title>
</head>
Then, between body elements, access through events. For example, you want to access the javascript function through the onclientclick of button1.
Examples are as follows:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('XXX')" />
When you run the project, click button, "Your name is XXX" will be displayed
This is a simple javascript function.
2. Called through js file in the foreground
The method is the same as (1) but you need to specify the .js file
Examples are as follows:
<head runat="server">
<script type="text/javascript" src="">
</script>
<title>using javascript</title>
</head>
Then, between body elements, access through events. For example, you want to access the javascript function through the onclientclick of button1.
Examples are as follows:
//At this time, the ShowName method must be in the .js file
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('XXX')" />
3. Call javascript function in the background, and the function is in the .js file
The head element of the front desk
<head runat="server">
<script type="text/javascript" src="">
</script>
<title>using javascript</title>
</head>
The following code needs to be added to the background
("onclick", "showname1(XXX)");
4. Call javascript function in the background. The function is written in the .js file, but not defined in the foreground.
//Get .js file
string myscript = "";
//Register .js file. If you view the source code at this time, you will get the following code
//<script> src ="" type="text/javascript"><script>
("myKey", myscript);
//Same as above
("onclick", "showname1(123)");
5. Write scripts using methods
For example, after you click the button, first operate the database, and after finishing, it is displayed. You can write it on the place you want to call it.
("<script type='text/javascript'>alert();</script>");
One of the flaws of this method is that it cannot call custom functions in the script file, it can only call internal functions. Specifically calling custom functions can only write function definitions, such as ("<script type='text/javascript'>function myfun(){...}</script>");
6. Dynamically add scripts using ClientScript class
The usage is as follows: Add code where you want to call a certain javascript script function, be careful to ensure that MyFun has been defined in the script file.
((), "myscript", "<script>MyFun();</script>");
This method is more convenient than that, you can directly call custom functions in the script file.
Note that among all the above methods, the background code cannot have code that converts the current page, such as Redirect, etc., and the page redirect code should be placed in the script.
It's very simple, add script elements between the head elements and set the type element to "text/javascript"
like:
Copy the codeThe code is as follows:
<head runat="server">
<script type="text/javascript" >
function ShowName(str)
{
alert("Your name is:("+str+")");
}
</script>
<title>using javascript</title>
</head>
Then, between body elements, access through events. For example, you want to access the javascript function through the onclientclick of button1.
Examples are as follows:
Copy the codeThe code is as follows:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('XXX')" />
When you run the project, click button, "Your name is XXX" will be displayed
This is a simple javascript function.
2. Called through js file in the foreground
The method is the same as (1) but you need to specify the .js file
Examples are as follows:
Copy the codeThe code is as follows:
<head runat="server">
<script type="text/javascript" src="">
</script>
<title>using javascript</title>
</head>
Then, between body elements, access through events. For example, you want to access the javascript function through the onclientclick of button1.
Examples are as follows:
//At this time, the ShowName method must be in the .js file
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="ShowName('XXX')" />
3. Call javascript function in the background, and the function is in the .js file
The head element of the front desk
Copy the codeThe code is as follows:
<head runat="server">
<script type="text/javascript" src="">
</script>
<title>using javascript</title>
</head>
The following code needs to be added to the background
("onclick", "showname1(XXX)");
4. Call javascript function in the background. The function is written in the .js file, but not defined in the foreground.
Copy the codeThe code is as follows:
//Get .js file
string myscript = "";
//Register .js file. If you view the source code at this time, you will get the following code
//<script> src ="" type="text/javascript"><script>
("myKey", myscript);
//Same as above
("onclick", "showname1(123)");
5. Write scripts using methods
For example, after you click the button, first operate the database, and after finishing, it is displayed. You can write it on the place you want to call it.
("<script type='text/javascript'>alert();</script>");
One of the flaws of this method is that it cannot call custom functions in the script file, it can only call internal functions. Specifically calling custom functions can only write function definitions, such as ("<script type='text/javascript'>function myfun(){...}</script>");
6. Dynamically add scripts using ClientScript class
The usage is as follows: Add code where you want to call a certain javascript script function, be careful to ensure that MyFun has been defined in the script file.
((), "myscript", "<script>MyFun();</script>");
This method is more convenient than that, you can directly call custom functions in the script file.
Note that among all the above methods, the background code cannot have code that converts the current page, such as Redirect, etc., and the page redirect code should be placed in the script.