Include File In ASP
Tips >> ASP


These article is based on Include File In ASP. The include option is the heart of making efficient ASP files and re-usable portion. You can place all of your commonly used code in one simple file and include it in any number of other ASP files you want. So when you need to update one piece of code that is used throughout your application you can simply update it in one file ie the include file and every other page that uses the include file will automatically be updated. The include file can come in handy for such things as functions, headers and footers and the connection string. Believe me it can save a lot of maintenance and updating time.The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages. Note :- Included files are processed and inserted before the scripts are executed.

The below are the Syntax for Including Files :-

<!--#include virtual="somefilename"-->
or
<!--#include file ="somefilename"-->

There are Two way of using an include file :-

  • Virtual, using the 'virtual' keyword
  • Relative, using the 'file' keyword
  1. Virtual, using the 'virtual' keyword :- Use the virtual keyword to indicate a path beginning with a virtual directory. If a file named "header.inc" resides in a virtual directory named /html, the following line would insert the contents of "header.inc":

    Example#1 :-

    Using the code below assumes that the file 'myfile.asp' is located in the root directory.

    <!--#include virtual="/myfile.asp" -->


    Example#2 :-

    When building my sites I sometimes put all my include files in an include folder called 'includes'. If I wanted to call a file called 'myfile.asp' in the includes folder with the virtual keyword then it would look as stated below :-

    <!--#include virtual="/includes/myfile.asp" -->

  2. Relative, using the 'file' keyword :-Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file. If you have a file in the html directory, and the file "header.inc" resides in html\headers.

    Example#1 :-

    The file keyword indicates a relative path so if the include file was in the folder 'test' then the relative path from the current folder might be :-

    <!--#include file="../test/yourfilename.asp" -->

    Example#2 :-


    If the file was in the same folder then it would simply be as stated below :-

    <!--#include file="yourfilename.asp" -->


    Note :- You can also use the file keyword with the syntax (..\) to include a file from a higher-level directory.
Let's take a look at Dynamic Include File :-I think that at one time or another all of us have attempted to create dynamic include files in our ASP applications only to find out that it would not work. The usual approach was to use a variable to hold the name of the file that you wanted to include and then pass the name of that variable to the include directive. Let us be clear: The following code will not work :-
<%
'A variable is declared to hold a file name
Dim MyFile

'The desired file name is passed to the variable.
MyFile = Request("SomeFileName")

'Then the variable holding the file name is passed'to the include directive.
%>
<!--#include file=<%=MyFile%>-->

 

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

+91-9967648641

+91-9967648641