How To: Include File in ASP.NET Pages
Posted by triaslama on November 19, 2008
Sometimes programming its just the matter of style, I am not generalize but often I found that we can achieve one goal through many different ways. Lets inspect one of those! By right now I have try three different way on how we put a file in ASP.NET pages. I don’t know which one is the best, but all of the three has the same meaning: we can refer to a file and bring it where ever we like in our ASP.NET pages.
Yes, this is maybe the prefered way because if we include a file and we place it in all of pages -let’s say copyrights footer for example- Later if we want to change the copyrights with the new one, or modify it to something we want, we simply modify it in one place (in our referenced file) rather than go through all of our pages and change it one by one.
How we do this? I have tried three solutions to do that.
1. Using <!– #include file=”[filename].aspx” –>
The first (and maybe the old one) 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”)
Much like the first one, but 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.
I am sorry if I’m not bloated this post with appropriate code. You know write a proper code here sometimes spend some of my leisure time (beside that I feel someone in Redmond has done that for you), and please note this post not devoted to examine which one is the best or which one is worst, the choice is yours. By right now I just want to say that sometimes coding is just the matter of style.