How to include file in ASP net page
There are at least 3 ways to do so:
1. Using <!– #include file=”[filename].aspx” –>
The first is we just write a simple script (as show above) and specify the file we want to include. Place the above script where ever we want content of [filename].aspx appear in pages that referenced it. For security reason always try to include only *.aspx files.
2. Using Response.WriteFile(”[filename].aspx”)
Just like the first one, but now we must place it in dynamic script. We need to place the above code inside <%%>. The example maybe look something like that
<div><% Response.WriteFile(”inc.aspx”); %></div>
3. Using Master Pages
Slightly different with the above two, we need a bit more step if we choose to use master page. First we must define the master page (file with extension *.master), so we need the following directive for our master page:
<%@ Master Language=”C#” %>
We can change the value for Language attribute with whatever language supported in .NET, and we can add attribute like CodeFile and Inherits too (for code behind style). In master page we define portion of page that can be replaced inside <asp:ContentPlaceHolder id=”contHolder” runat=”server”></asp:ContentPlaceHolder>. Yes this is a new stuff (not really new, but its emerged in .NET 2.0).
As second step in using master page, we must define page(s) that will use the master page (*.aspx files). We will need the following directive:
<%@ Page Language=”C#” MasterPageFile=”[masterfile].master” %>
And we place the content of master page (portion inside <asp:ContentPlaceHolder /> control) with the following code:
<asp:Content id=”content1″ ContentPlaceHolderID=”[placeHolderID]” ></content>
To make it work make sure we don’t forget ContentPlaceHolderID, it must has value that associated with ID of ContentPlaceHolder control in master page