One method ASP.NET can use to access files is called file inclusion. This method simulates taking the content of one file and inserting it into another; luckily, you don't have to actually do that. The methods I'll talk about in the following sections show you different methods to include files.
A server-side include file is a way to separate code from a page. This is helpful if you ever need to reuse that code, or simply want to make it easier to edit your pages.
The syntax for using server-side includes is as follows:
The keyword virtual is used to specify a file by virtual directory, whereas file specifies a file path in the local directory structure. For example, if you want to include the contents of the file in http://www.yourserver.com/includes/file1.aspx in an ASP.NET Web page running from http://www.yourserver.com/someDir1/someDir2/someFile.aspx, you could use either one of these #include statements:
<!--#include file="../../includes/file1.aspx"--> <!--#include virtual="/includes/file1.aspx"-->
Note that the #include file statement must be changed depending on the directory in which the ASP.NET page making the server-side include call is located, relative to the include file. These type of includes are called relative file paths. This can get frustrating as files move around, so ASP.NET allows you to also specify includes with absolute directories as well, using the #include virtual syntax, as shown in the second statement in the previous code snippet. This allows you to specify the path for a server-side include, regardless of where the calling ASP.NET pages are located.
Let's encapsulate a commonly used header in a server-side include that you can reference from your ASP.NET pages. Start by creating a file named header.aspx and placing the following code inside it:
<a href="index.aspx"><img src="/books/4/226/1/html/2/header.gif" alt="Home"></a><hr> <a href="index.aspx">Home</a> <a href="logout.aspx">Log out</a>
Don't encapsulate any other tags, such as <form> elements; these will be contained in the calling ASP.NET page. Now let's create the following ASP.NET page in the same directory, and include the header:
<script language="VB" runat="server"> Sub Page_Load(obj as Object, e as EventArgs) 'do something End Sub </script> <html><body> <!--#include file="header.aspx"--> Hi there! </body></html>
ASP.NET inserts the content from header.aspx into our file before it processes any ASP.NET commands. Therefore, when the page is ready to be executed, ASP.NET thinks it looks like this:
<script language="VB" runat="server"> Sub Page_Load(obj as Object, e as EventArgs) 'do something End Sub </script> <html><body> <a href="index.aspx"><img src="/books/4/226/1/html/2/header.gif" alt="Home"></a><hr> <a href="index.aspx">Home</a> <a href="logout.aspx">Log out</a> Hi there! </body></html>
This listing will produce the page shown in Figure 13.1.
Figure 13.1. Server-side includes are treated as if they were part of the calling ASP.NET page.
Remember that all this happens on the server, before anything is sent to the browser. The files are merged on the server, before any code is executed, and before anything is sent to the client. Therefore, a Response.Redirect would still work.
We can use a server-side include anywhere in the page, so this header could have just as easily been a footer. It can even include tags such as <html> and <body>, or ASP.NET server controls.
By default, server-side includes can be used only from files with the extensions .shtml, .shtm, .stm, .asp, .asa, .asax, and .aspx. When used in an .html file, for example, the include will not be processed. You can change these extensions with the Internet Services Manager in IIS 5.0.
Server-Side Includes Versus Other Includes
You might be wondering about the difference between a server-side include and a user control. Both can be used for identical reasons: to encapsulate portions of UI. The difference is that user controls are for displaying UI, whereas server-side includes can be used for any purpose, including encapsulating commonly used functions or constants.
Typically, if you want to encapsulate a UI element, it is a better idea to do it in a user control because it can have programmatic elements that you may manipulate, whereas you can't do this using a server-side include.
|Do ||Don't |
|Do use server-side includes when you want to include commonly used programmatic elements, such as classes or functions, in your pages. ||Don't use server-side includes when you are only encapsulating UI use a user control instead. |
There are other methods to include files in ASP.NET most of which we have already covered. For code-behind forms, see Day 19, "Separating Code from Content." For the import keyword, see Day 2, "Building ASP.NET Pages." Finally, for user controls, see Day 5, "Beginning Web Forms."
These methods vary in their implementations, but each allows ASP.NET pages to access the contents of another file.