ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @ Microsoft.com
What is Adovbs.inc and Why Do I Need It?

QUICK TIP:
Always Put "s Around Your Data
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


A Simple Text File Reading User Control

by John Peterson

Introduction

With all the ASP.NET samples I've been writing recently, I decided I should spend some time working on the publishing process. It sucks... for both myself and for our readers. It currently takes me almost as long to publish a sample as it does to write one! There are so many places I have to change things that I always forget one of two. Anyway that's where this code comes from.

I was looking for an easy way to publish the source code to the ASP.NET samples running on our site, but like our classic ASP samples, I wanted the code to come from the script itself. Here's the temporary solution I'm using while I get everything else working. It doesn't do color coding or anything fancy, but before I went and confused everyone I thought people might benefit from seeing a basic script implemented as a user control.

The Code

Here's the code as it currently stands. It doesn't really have any explanation or comments, but it's pretty straight-forward.

The first listing is for the control itself. There are a couple things important to note here.

  1. First off I'm using databinding. You normally think of using it for datagrids and the like, but you can use databinding to attach values to almost anything in .NET. It tends to make for very simple and straightforward looking pages. No messing with assigning strings to properties of label objects... just a simple layout with the values plugged in where they go.

    There is a slight downside... you need to call DataBind. You could easily do this in the control, but I do have a reason for not doing so. I am planning to include quite a number of this type of control on any given page. Instead of calling the databind command at the control level for each control... I'm going to do it once at the page level and it will automatically cascade down to every control on that page... pretty neat huh?

  2. The second thing of interest here is the caching. Reading off the file system can be an expensive and time consuming operation when you start talking about any real traffic. To combat this I have the control set to output cache just like you would do with a slow running ASP.NET page. The result is that the control's output is saved in memory and served directly without re-reading the contents of the file. Give it a try... change one of the source files once the control is cached... the control will continue to output the old contents until the cache expires.

srcview.ascx
<%@ OutputCache Duration="3600" VaryByParam="none" %>
<script language="VB" option="explicit" runat="server">
  Public FileName As String
  Function GetFileContents(FileName As String)
    Dim objFile         As System.IO.File
    Dim objStreamReader As System.IO.StreamReader
    objStreamReader = objFile.OpenText(Server.MapPath(FileName))
    GetFileContents = Server.HTMLEncode(objStreamReader.ReadToEnd())
    objStreamReader.Close()
  End Function
</script>
<table cellspacing="0" cellpadding="0" border="1">
<caption><em><strong><%# FileName %></strong></em></caption>
<tr><td>
<pre>
<%# GetFileContents(FileName) %>
</pre>
</td></tr>
</table>

I couldn't very well stop there now could I? What good is a control if you don't use it? Here's a simple page that creates two instances of the control each passed it's own value for the source of the file to read in.

Everything here is pretty basic... the only thing that really needs mentioning is the Page.DataBind conmmand in Page_Load. This is what I was talking about earlier. By placing the command here it will trickle down to all the controls on the page and will databind everything at the same time.

default.aspx
<%@ Page Language="VB" %>
<%@ Register TagPrefix="asp101" TagName="SourceViewer"
	Src="controls/srcview.ascx" %>
<script language="VB" option="explicit" runat="server">
  Sub Page_Load(Src as Object, E as EventArgs)
    Page.DataBind
  End Sub
</script>
<html>
<head>
<title>ASP 101 - A Simple Text File Reading User Control</title>
</head>
<body>
<p>
Below are two instances of the user control.
The first reads in and displays the source of
this file, while the second does the same thing
for textfile.txt.
</p>
<asp101:SourceViewer runat="server" FileName="default.aspx" />
<asp101:SourceViewer runat="server" FileName="textfile.txt" />
</body>
</html>

As you can see it's pretty simple, but hopefully it'll be helpful to someone. If you don't have a use for the control itself, at least it might serve as a simple example of databinding. The more I work with .NET, the more I realize that databinding is one of the most important concepts in the .NET world.

Download

You can download a zip file containing the sample scripts listed above from here: srcview.zip (1.6 KB).


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers