After reading ASP code, you will know that include is very common, and is rarely used in Aspx. In the ASP era, in order to avoid repeated work, a file is often made for areas or codes with similar functions, and then connected (directly connected or virtual) the method is introduced into the ASP Webpage file. For a large referenced ASP file, it is equivalent that a file is divided into many blocks, data between files can be freely shared (except for data in functions ).
The user control of aspx is very different from that of include. The biggest feature of aspx is that it is presented in the form of encapsulated objects. Through our programming, A common example can be abstracted, and some functions and methods can be summed up to form corresponding functions and properties for external event calls, so as to implement full classization, it encapsulates the internal facts that outer users do not need to know to the greatest extent, making them more maintainability, improving data security, and facilitating program release.
Simple things are easy to use, but naturally there are few functions, or they are not perfect. We can freely include a file into a specified ASP file. However, because the data of each other can be shared, the file dependency is high, which reduces the code readability and is not conducive to system maintenance. At the same time, if there is an image in the file to be included and the file to be included is not in the same directory, the image address of the included file must be the address of the image displayed in the file to be included, such as/file/index. ASP is the main file (the file that needs to contain the file), And/file/include/title. ASP is an included file, and the image address in title.aspis changed to title.jpg instead .. /title.jpg. Otherwise, the image cannot be displayed in the main file, and the hyperlink is the same.
Complex aspx solves many problems. For example, you can freely set the image address in the control file, so you don't have to worry about the directory under which the file is referenced, as long as the items that can be displayed in the control file can still be used after being referenced, this makes the controls made by users more reusable, unlike ASP, when files under different directories reference files with the same function, you need to set two files with actually the same but different image or hyperlink addresses. resources are wasted, and maintenance costs are higher. of course, these advantages also make file writing more troublesome, and have higher requirements on analysis, making the control more widely used.
Therefore, include is rarely used in Aspx. It is generally used to replace include with controls. Although aspx already has controls, they are very powerful. However, aspx still inherits the include in ASP, so that aspx can support the original ASP. Of course, the usage in aspx is exactly the same as that in the original ASP. For example, this page contains the header file of head. aspx <! -- # Include file = "head. aspx" -->. This includes copying the code in the head. aspx file to the page. Of course, if you use codebehind, the code in head. aspx. CS will not be included. Because include is a simple copy operation, it is not as intelligent as controls. Obviously, if you want to include a pure static code, it is good to use include, which reduces unnecessary processing than space. Another point is also visible in the editor when using include, which is helpful for layout.
Let's look at a simple example on the Internet:
Code implementation:
[Part of the above file: head. aspx]
<SCRIPT runat = Server>
Void click1 (Object A, eventargs B)
{Label1.text = text1.text;
Label2.text = text2.text ;}
</SCRIPT>
<H1> the softzz's new page
<P> 2004-11-15 </P>
Name: <asp: textbox id = "text1" runat = "server"/>
Pass: <asp: textbox id = "text2" runat = "server"
Textmode = "password"/>
<Asp: button id = "button1" runat = "server"
TEXT = "clickme" onclick = "click1"/>
& Lt; HR width = "80%" size = "1" & gt;
[Next part of the file: end.]
<SCRIPT runat = Server>
Void click2 (Object A, eventargs B)
{Label1.text = text3.text;
Label2.text = text4.text;
}
</SCRIPT>
& Lt; HR width = "80%" size = "1" & gt;
Name: <asp: textbox id = "text3" runat = "server"/>
Pass: <asp: textbox id = "text4" runat = "server"
Textmode = "password"/>
<Asp: button id = "button2" runat = "server"
TEXT = "clickme" onclick = "click2"/>
<H5> <% = datetime. Now. tostring () %> </H5>
<B> <p> copyright: softzz </P> </B>
[Main file: Index. aspx]
<% @ Page Language = C # %>
<Center>
<Form runat = Server>
<! -- # Include file = "head. aspx" -->
<Br/>
<P> This is a new test page. Please look at the info: </P>
<Br/>
User's name: <B> <asp: Label id = label1 runat = server/> </B>
<Br/>
User's Pass: <B> <asp: Label id = label2 runat = server/> </B>
<Br/>
<! -- # Include file = "end. A" -->
</Form>
</Center>
What else can I explain in the above example?
Include can combine several files into one file. Each page of an element is a part of the final page.
The spelled pages and final pages are ASP. NET code containers, rather than HTML text.
In order.
When the code in these files is spelled together, it is only common text. After the code is finally assembled, it will be checked, compiled, reported, and displayed ......
One file can include another file multiple times. However, the premise is that the "Declaration/Definition" (identifier) part cannot be duplicated (duplicate ).
If there is a "runat = server" Control in each file, pay attention to the Start/end position of <form runat = "server">.
Only one <form runat = "server"> can be displayed on the page, and no more than one form ID can be set.
<SCRIPT runat = "server"> can appear multiple times on one page, and it will all run before the "final page" is displayed.
In addition, its operation is irrelevant to the page sequence. It is a "code declaration block" and its elements will only run after being called.
We can place <SCRIPT runat = "server"> in any part of the page, including <form runat = "server">.
Use the include method to reference files. The extension can be arbitrary.