Geekpedia Programming Tutorials






Using server side includes

Demonstrates how to use server side includes in ASP .NET by including User Control files in Web Forms.

On Monday, August 16th 2004 at 02:53 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.8 with 13 votes)
Contextual Ads
More ASP.NET Resources
Advertisement

Edit: For those who want a quick solution, to include an aspx file into another aspx file use the following:


<!-- #Include File="fileName.aspx" -->



Almost all the time you have to use server side includes in web applications. While in PHP you can include a file using the following piece of code:





include("fileName.php");



...in ASP .NET it's a bit more complicated, but more interesting.



Create a new ASP .NET Web Application project named includes. Along with the files in the current project, add a new file that will be included in WebForm1.aspx. The file has to be a Web User Control (with the ascx extension).

So right-click on the project in the Solution Explorer window and choose Add -> Add New Item.... From this window choose the Web User Control:








Click Open after you leave the default name (WebUserControl1.ascx). Along with our Web Form - WebForm1.aspx, is now our User Control - WebUserControl1.ascx:







Open the file WebUserControl1.ascx and inside the source use the following:





<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="includes.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>



<
p>I'm a server side included file...</p>



The first line (the directive) is created by Visual Studio .NET anyway, so you can only add the paragraph.

Now open the main Web Form - WebForm1.aspx - in HTML view. In the body tags add the following tags:





<%@ Register TagPrefix="Include1" TagName="DefaultInclude" Src="WebUserControl1.ascx" %>

<Include1:DefaultInclude ID="myInclude" Runat="Server" />



In the first line a directive is created where we register the user control. It contains a TagPrefix attribute which defines a namespace that you'll use to get access to the User Control, a TagName that acts as an alias for the class and the Src attribute which contains the path to the user control file.

The second tag is the actual usage of the control in the file, where the second tag is inserted, the content of the User Control file will be displayed.



User Control properties


Maybe you want to display something in the included file different on other pages, depending on the Web Form that includes the User Control. This technique is often used when including headers, so you can display the Title of the webpage (inside <title></title> tags) depending on the Web Form even if the header included file is the same. Let's look at an example.

Replace the paragraph inside WebUserControl1.ascx with this one:





<p>I'm included in the <%=FileName%> file.</p>



Where FileName resides, the actual name of the file will be. But before we assign a string to FileName inside WebForm1.aspx, we need to declare the variable inside WebUserControl1.ascx.cs. Also here we can set a default value for the variable, in the case in which it is not set in WebForm1.aspx. So open WebUserControl1.ascx.cs and declare the variable FileName:





public string FileName = "unknown";



Now inside WebForm1.aspx finally set FileName to the name of the file. You do this by adding the attribute FileName to the tag:





<%@ Register TagPrefix="Include1" TagName="DefaultInclude" Src="WebUserControl1.ascx" %>

<Include1:DefaultInclude ID="myInclude" FileName="WebForm1.aspx" Runat="Server" />



This is the result:



Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by dfggfdgf on Wednesday, September 22nd 2004 at 07:36 PM

Could have shown how to do includes outside the contexts on the web controls.

by Andrei Pociu on Sunday, September 26th 2004 at 12:38 PM

Ok, I added a line on how to quickly include a file.

by Stephan on Monday, December 19th 2005 at 10:29 AM

Would be nice with files ready for download on the tutorial.

by Michal on Friday, June 9th 2006 at 02:12 PM

Can I do that:
<!-- #Include File="fileName.aspx" -->
in C# and how?

by Michal on Friday, June 9th 2006 at 02:12 PM

* Can I do that quick file include in C#?

by hemant on Monday, July 31st 2006 at 06:14 AM

i want to now
how we
include .aspx pages automatically in solution explorer without refreshing the solution explorer


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons