Read, With the Name of Your Lord Who Created

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.

8 Responses to “How To: Include File in ASP.NET Pages”

  1. Benny Halperin said

    By far, Master page is the preferred method. It was created for designing a uniform template across sites/apps. Header/footer are only classical examples.

  2. Ganesh said

    Thanks the article was really helpful.

  3. Thanks just what I was looking for🙂

  4. AMIT said

    but how to include file name dynamically.for e.g. if code(variable) contain different
    value (about 500 one at a runtime) then how we include file_name .I try

    but this is not working and i can’t apply if-else or case statement for such a large database.

  5. KICS said

    Gr8 and GR8 thankx

  6. soumya said

    Very helpful…clead description.
    Thanks

  7. Hello colleagues, its fantastic piece of writing on the
    topic of cultureand fully explained, keep it up
    all the time.

  8. You know what, your site content are usually a good examine nonetheless this time
    around I find myself enjoy it is put with each other somewhat rash.
    I’d become drastically wrong with that nevertheless it just simply doesn’t move similar to various other content. Sorry this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: