There are a number of ways to introduce JavaScript in asp.net. In the beef brisket when the main talk about two kinds.
Personal opinion can be divided into direct introduction, and indirect introduction.
first, Direct introduction。 Invoke a custom JavaScript function on the foreground page:
1. Open the foreground page to add a SCRIPT element between the head elements and set the type element to "Text/javascript". Then write JavaScript-customized functions.
Copy Code code as follows:
<script type= "Text/javascript" >
function ShowName (str)
{
Alert ("News Category: (" +str+ ")");
}
</script>
<title>using javascript</title>
2, the body elements, through the event to access, such as through the Button1 Click event (OnClientClick) to access the JavaScript function of the shwoname () examples are as follows:
Copy Code code as follows:
<span style= "font-size:18px" ><asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "ShowName (' China News ') "/></span>
When you run the project, when you click the button, the News category: China News, which is the direct definition and invocation of JavaScript functions on the front page.
second, indirect introduction。 In the foreground by introducing a JS file to call the corresponding function.
The same as the direct access step, are required to be introduced first in the call. Because the direct introduction is the JavaScript function written in the foreground interface, the indirect reference is written in a JS file, so the indirect reference needs to indicate the JS file to call.
The code is as follows: The red section indicates the JavaScript file.
Copy Code code as follows:
<script type= "Text/javascript" <span style= "COLOR: #ff0000" >src= "Changgetype.js" ></SPAN>
</script>
<title>using javascript</title>
Once the definition is finished, it is called in the body.
PS: Both methods can be introduced. Direct introduction may start to write more simple, intuitive, but the JavaScript code and HTML tags directly in front of the page, the code a lot, the page becomes bloated, and later to change also trouble, flexibility is not good. Therefore, the direct introduction of this method is suitable for small projects, maintenance, and modification is not large.
It is recommended that you do programming, bar JavaScript code into a separate JS file, other pages by introducing the JS file to use the corresponding JavaScript code, flexible operation, maintenance, changes are convenient.
Here are two ways to introduce JavaScript from the foreground, and there are a lot of ways to access JavaScript from the background, not yet asp.net.