Computer Books Online Computer Books Online Computer Books Online Computer Books Online
 
Computer Books Online
HomeSite Map
Site Map
Computer Books Online Home Search Browse Used Books Customer Service Computer Books Online  
My Order View Cart Check Out Contact Us
Find Books
Find Books Find Books
Find Books
Advanced Search Advanced Search
Advanced Search
Categories Categories
Categories
Coming Soon Coming Soon
Coming Soon
On Sale On Sale
On Sale
Used Books Slightly Worn
Used Books
New Releases New Releases
New Releases
Content Content
Content
Chapters Articles
Chapters
Blog Blog
Chapters
Chapters Chapters
Chapters
Contest Contest
Contest
Free Computer Mags Free Computer Mags
Free Computer Mags
Laugh Laugh
Laugh
Tips Tips
Tips
Mailing List Join Our Mailing List
Mailing List

Your Email Your Email:
Your Email
Subscribe Subscribe
Update Update
Remove Remove

Join Mailing List

Sample Newsletter About Mailing Lists
Company Information
Company Information Company
Computer Books Online
Contact Us Contact Us
Contact Us
Customer Service Customer Service
Customer Service
Policies and Procedures Policies and Procedures
Policies and Procedures
Privacy and Security Privacy and Security
Privacy and Security
International International
International
Computer Books Online

 

 
Tips Categories Active Server Pages
 
 

Search All Active Server Pages Tips:  



Page 3 of 10
 
[ 1 2 3 4 5 6 7 8 9 10 ]

            Determining whether a file exists in ASP.NET

            Determine the amount of free space on your Web server

            ASP.NET makes uploading files from the client to the server a snap

            Allow users to upload files from the client to the server with ASP

            Displaying the last modified date in ASP.NET

            Defer client-side scripts for a better user experience

            How do I host ASP or ASP.NET Web sites using Microsoft Windows XP Home Edition?

            Add leading zeros to a number in ASP

            How to display the euro symbol under ASP

            The VBScript IsNumeric function doesn't ensure *only*

            What to do when your ASP pages can't access WMI

            Comparing Server.CreateObject and the object tag in ASP

            Use the keyboard instead of the mouse to quickly cycle through

            Help! My ASP Session or Application variables have

            Handling the Invalid Default Script Language error in ASP

 Determining whether a file exists in ASP.NET


In many Web applications, you may need to check for the existence of a particular file before you attempt to do something with it. The alternate approach--attempting to access the file first and relying on an error handler to catch the scenario where it doesn't is serviceable, but in our opinion, it's sloppy coding. Determining whether a file exists in ASP.NET is very simple, thanks to the classes in the System.IO namespace. Take a look at this code snippet, which demonstrates how easy this is to accomplish:

<%@ Import Namespace="System.IO" %>

<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If File.Exists("c:\test.txt") Then
Response.Write("File c:\test.txt does exist")
Else
Response.Write("File c:\test.txt does *not* exist")
End If
End Sub
</script>

UP



 Determine the amount of free space on your Web server


There might be oocasions when you want to be able to determine and display the amount of disk space remaining on your Web server from an ASP page. For example, you might have developed a suite of Web server administration pages for your own internal use. Whatever the reason, this isn't difficult to accomplish. Take a look at the following code snippet for the answer:

UP



 ASP.NET makes uploading files from the client to the server a snap


In a previous tip, we talked about uploading files via classic ASP. As you saw, getting the file from the client to the server isn't a big deal, but getting ASP to handle the file properly, once delivered, was no walk in the park. Fortunately, like most other tasks that were once tedious or impossible in ASP, ASP.NET makes this task simple.

All you have to do is provide the interface for the upload, in the same way you would do with ASP--the <INPUT type=file> tag corresponds in ASP.NET to a server-side System.Web.UI.HtmlControls.HtmlInputFile control, which is rendered in HTML in exactly the same way. Then, in your code-behind page, use something like the following intuitive code:

Protected Sub UploadButton_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles UploadButton.Click
Dim FileUpload As HttpPostedFile
Dim UploadedFileName As String

'Get the file from the HtmlInputFile control
FileUpload = UploadInterface.PostedFile

If Not UploadInterface.PostedFile Is Nothing Then
'Get just the filename (without the client path)
UploadedFileName =
FileUpload.FileName.Substring(FileUpload.FileName.LastIndexOf("\"))
'Save it in a safe place on the server
UploadInterface.PostedFile.SaveAs("c:\temp\" & UploadedFileName)
End If
End Sub

UP



 Allow users to upload files from the client to the server with ASP


Many Web sites need to provide users with the ability to upload documents from their Internet browsers to your Web server. With HTML, you can provide an interface to do so in your Web pages by using the <input> tag with a type attribute of "file", as shown here:

<form method="post" name="MyForm" action="ReceiptPage.asp"
enctype="multipart/form-data">
<input type="file" name="Filename">
<input type="submit" value="Upload Now">
</form>

But this only provides the interface with which users on the client side can select the file and submit it to your ASP page. Using ASP, it isn't terribly easy to handle the form's contents once they're received on the server. We've covered a way to do so using pure ASP in Active Server Developer's Journal, but it's an advanced and detailed solution (see "Upload files with
HTML forms and pure ASP" in the October 2001 issue). But the simplest way to handle the file in ASP is to use a third-party component. Here's a sampling of just a few of the ones you might use:

ActiveFile from Infomentum:
www.infomentum.com/activefile/

AspUpload from Persits Software:
www.aspupload.com

aspSmartUpload from Advantys:
www.aspsmart.com

Posting Acceptor (or other components) from Microsoft:
www.microsoft.com/windows/software/webpost/default.htm

SAFileUp from SoftArtisans:
www.softartisans.com

UP



 Displaying the last modified date in ASP.NET


Many Web sites display the last modified dates. This helps customers understand how fresh the information is and Web administrators quickly see when the files were last updated. If you want to display the last modified date on every page of your Web site, you can create a user control, which will read and display the timestamp of the currently displayed ASPX file.

To add a user control containing the last modified date to your Web project, open the project in the Visual Studio.NET and follow these steps:

(1) Select the Add Web User Control option from the Project menu
(2) In the Add New Item dialog, name the file something like Footer.ascx
(3) Add a label Web control to the Footer.ascx file, e.g.:

<ASP:LABEL id="timeStamp" runat="server"></ASP:LABEL>

(4) Open the code-behind file and add the following code to the Page_Load method (in Footer.asx.vb):

Dim file As System.IO.FileInfo
file = new System.IO.FileInfo(Request.PhysicalPath)
timeStamp.Text = "Last modified: " +
file.LastWriteTime.ToShortDateString()

(5) Add the Register directive at the top of the ASPX files using the control, e.g.:

<%@ Register TagPrefix="MYCONTROL" TagName="FOOTER" Src="Footer.ascx" %>

(6) Reference the control from the ASPX pages, e.g.:

<MYCONTROL:FOOTER id="Footer" name="Footer" runat="Server">

UP



 Defer client-side scripts for a better user experience


How many times have you seen a syntax error pop up from the execution of client-side code when you know perfectly well that the script has been properly debugged and working? This sometimes happens when scripts attempt to access page elements that haven't finished loading yet. But there's a good way to address this problem: defer execution of client-side scripts. To do so, add the defer=true attribute to your script tag, like this:


<SCRIPT src="foo.vbs" defer>

This will not only solve the syntax error problems, but the page will often appear to load more quickly, since the browser doesn't have to parse the client-side script until after it's finished rendering all of the visual layout of the page.</body>
</html>
</SCRIPT>

UP



 How do I host ASP or ASP.NET Web sites using Microsoft Windows XP Home Edition?


The short answer is: You don't. Microsoft doesn't provide or support IIS with the Home Edition of Windows XP, and although there are some posted workarounds to getting IIS installed and running with XP Home, we can't endorse or recommend them. The best solution is to upgrade XP Home Edition to XP Professional. It isn't expensive, and you won't run the risk of breaking something in an effort to force XP Home Edition to do something Microsoft hasn't designed it to do.

UP



 Add leading zeros to a number in ASP


Sometimes you want to add a certain number of zeros in front of a number using VBScript. For instance, you've got the number "567", and what you want to display is "000567". Here's a simple function that can do this for you:

Function AddLeadingZeros(NumberToPad, TotalLength)
AddLeadingZeros = NumberToPad
If TotalLength > Len(NumberToPad) Then
AddLeadingZeros = String(TotalLength - Len(NumberToPad), "0") & NumberToPad
End If
End Function

And you can use it like this:

Response.Write(AddLeadingZeros(567,6))

UP



 How to display the euro symbol under ASP


Since it's relatively new, a lot of developers aren't aware of how to display the euro symbol in their Web pages. It's actually quite easy. Just use either of the following HTML entities:

Response.Write "€"
Response.Write "€" 'Equivalent and more readable!

UP



 The VBScript IsNumeric function doesn't ensure *only*


You might have wondered why the IsNumeric function returns True for some Strings that contain characters other than the digits 0-9. It's because the function's job isn't to determine whether a String contains *only* numbers, but rather, to determine whether a String can be *evaluated* as a number. As such, dollar signs and other currency symbols, commas or other regional digit delimiters, and other characters and character combinations (for example, "&H") can all still point to numeric values. To determine whether a String contains *only* numbers, you'd have to write your own IsNumeric function.

UP



 What to do when your ASP pages can't access WMI


As you may know, Windows Management Instrumentation (WMI) is an extremely powerful set of components for managing Windows from script. There are many worthwhile applications of WMI that you can develop using Active Server Pages, but you need to know that, by default, WMI isn't enabled for access from ASP. This is for security reasons, but when you want to enable it, here's how:

Launch the Microsoft Registry Editor and locate the following key:

HKEY_LOCAL_MACHINE SOFTWARE
Microsoft WBEM Scripting

Then, change Enable for ASP from its default value of 0 to a new value of 1. This should enable your ASP scripts to access WMI and all it has to offer.

UP



 Comparing Server.CreateObject and the object tag in ASP


As you may know, there are two ways you can create a COM object
in Active Server Pages. These two equivalent methods are shown here:


<%
'Using Server.CreateObject
Dim fso
Set fso = Server.CreateObject _("Scripting.FileSystemObject")
%>

<!-- Using the <object> tag -->
<OBJECT id=fso runat="server" progid="Scripting.FileSystemObject"></OBJECT>

Apart from being slightly more succinct, the <OBJECT> tag method
has another advantage. When you use Server.CreateObject, the COM
object is instantiated immediately, whether you actually end up
using it or not. With <OBJECT> , on the other hand, the object
isn't instantiated at all until the first time you set one of its
properties or call one of its methods. This can help you converse
memory in an application where every byte counts. </OBJECT>

UP



 Use the keyboard instead of the mouse to quickly cycle through


Developers tend to type a lot, by the very nature of their jobs. But it can be distracting and tedious to have to go to the mouse every time you want to switch from one open code window to another as you work, so use the keyboard instead. You can use [Ctrl][Tab] to switch between open windows in Visual InterDev, as well as Visual Studio .NET. This simply keyboard shortcut can really make a difference if you keep many windows open at once.

UP



 Help! My ASP Session or Application variables have


Suppose you're working with an ASP Web application that depends upon Session
or Application variables and all of a sudden you realize they've disappeared.
What can cause this?

For Session variables, the most common cause is that
the Session has expired. You can configure an entire Web site as well as
individual Web applications with their own Session expiration time limits. The
default is 20 minutes. If the user leaves a browser idle for that
long--poof!--his Session variables will be destroyed.

For Application
variables, this usually happens if the Web site has been stopped and restarted,
if the Web server itself has been stopped and restarted, or if the Global.asa
file for the Web application has been changed. Since the first two would usually
result in the entire application being unavailable, check the last modified date
of Global.asa to see if a fellow developer has made a change without telling
you. You'd be surprised how often this happens!


UP



 Handling the Invalid Default Script Language error in ASP


There may be occasions, particularly when you deploy code onto one machine from another, where you encounter the following error: Invalid Default Script Language (or a slight variation on this wording). Why does this occur? First off, double-check your page directive for typos such as "VBScrip" or JScirpt". Of course, if the code was running on one machine successfully, then there's a different problem: The scripting language you've chosen may not be available on the target machine. To check, launch the Internet Services Manager, right-click on the Web site or virtual directory that's malfunctioning, and choose Properties. Then, on the Home Directory tab, click the Configuation button. Click on the App Options tab and examine the Default ASP Language text box. Ensure that a valid entry exists here. And, if this doesn't work, download the latest scripting engines from Microsoft at msdn.microsoft.com/scripting.

UP





Page 3 of 10
 
[ 1 2 3 4 5 6 7 8 9 10 ]

AddThis Social Bookmark Button

 

Free Computer and Technical Magazines!

 

 

 

 

Search I Book Index I Contact I Feedback
Copyright © 1997-2009 Computer Books Online
About Us I Publishers & Authors I Privacy Policy
All products and company names mentioned herein are the trademarks of their respective owners. No part of this website may be reproduced without the prior written permission of Computer Books Online. Prices and availability subject to change without notice..