How To Dynamically Include Files in ASP.NET (306575)



The information in this article applies to:

  • Microsoft ASP.NET (included with the .NET Framework) 1.0
  • Microsoft Visual Basic .NET (2002)
  • Microsoft ASP.NET (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2003)

This article was previously published under Q306575
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

192144 How To Dynamically Include Files in Active Server Pages

IN THIS TASK

SUMMARY

This article demonstrates how to dynamically include HTML and client-side scripts in .aspx pages.

Because ASP.NET applications are compiled and run before they are sent to the client, you cannot use a variable in place of a file name in a server-side include file (such as <!-- #include PathType = FileName -->"). However, you can use the Response or StreamReader object to write the include file to the HTTP content stream.

This article demonstrates how to create an .aspx page that reads a file with static HTML and/or client-side script code and writes the file's content to the browser.

back to the top

MORE INFORMATION

In ASP.NET, the Response object provides a new method named WriteFile. You can use the WriteFile method to write the specified file directly to an HTTP content output stream.

If you only want to write the content of a file to the browser, you can accomplish this in just one statement. If you want to manipulate the file before you send it to the browser, refer to the References section for information about basic file input/output in .NET.

In ASP.NET, you can either write inline code or write code in the code-behind module. This article presents an inline code sample that opens a file and writes the file's content to the browser.

back to the top

Steps to Create the Sample

  1. Open Microsoft Visual Studio .NET.
  2. From the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types. Under Templates, click ASP.NET Web Application.
  4. Switch to the HTML code editor for the .aspx page that is created by default. Replace the existing code with the following code:
       <%@ Page Language="vb" AutoEventWireup="false"%>
       <html>
       <body>
            <%		   
              Response.WriteFile ("Yourfile.inc")
            %>
       </body>
       </html>
    					
  5. Replace "Yourfile.inc" in the Response.WriteFile statement with the name of an include file that contains some HTML or client-side script.
  6. Add "Yourfile.inc" to the project.
  7. Browse to the .aspx file. Note that the content of your file is written to the browser.
back to the top

Troubleshooting

  • Server-side code in the dynamically included file is displayed on the client browser.

    The dynamically included file may contain any client-side code, including HTML and JavaScript. If that file contains any server-side code, the server-side code is sent to the client browser as plain text and is visible if you view the source of the page that is displayed in the browser. Note that ASP.NET does not process server-side script in the dynamically included file. This is because all of the ASP.NET code has already run before it includes the file; thus, the server does not return to read anything for server-side processing again.
  • If you use Response.Write or Response.WriteFile statements in a code-behind module, these statements write the information before any HTML tags. The same behavior occurs if you use inline <SCRIPT> tags with the RUNAT="Server" attribute.

    Because the code-behind modules are compiled first, all of the output that is generated by Response.Write, Response.WriteFile, or inline server-side <SCRIPT> tags appears before any HTML tags when the HTML output is sent to the browser. This problem does not occur when you use Response.Write statements in classic ASP-style tags.
back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

304427 How To Do Basic File I/O in Visual Basic .NET

304430 How To Do Basic File I/O in Visual C# .NET

back to the top

Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbHOWTOmaster KB306575 kbAudDeveloper