AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > ASP编程

使用ASP脚本技术

51自学网 http://www.51zixue.net

    在VB脚本中,你不必定义变量或者象在其他脚本语言中明确的定义他们的类型。一个变量在你第一次使用的时候存在。但是,这个特性让你的代码广泛的存在打字稿中。如果你在代码中错误的定义一个变量名,那么一个新的变量将被创建。你的脚本可能不能正常的工作,而你也有可能不能意识到这个错误。

转自:动态网制作指南 www.knowsky.com

在你使用变量的时候,你要养成定义他们的习惯,你所需要做的就是测试Dim variableName:

%<%Dim IntUserID%>%

IntUserID现在可以使用了。为了另外一个安全网, 使用Option Explicit. 如果你打开Option Explicit, 你将会在使用变量的任何时候发出错误的信号。这个听起来很乏味,但是当你的脚本发生错误的时候,他可以给你一些线索,否则你要艰难的找出错误出在哪里。

为了使用Option Explicit, 将下面的内容作为你脚本的第一行内容:

<% Option Explicit %>

如果你想要看看当你忘记定义了变量的时候会发生什么状况,可以运行下面这点代码:

<% Option Explicit %>
<:% strName = Request.Form("Name") %>

因为strName变量 (Dim strName) 没有被定义,你将会看到发生下面这些错误:

Microsoft VBscript runtime error '800a01f4'
Variable is undefined: 'strName'
/e/oe-test.asp, line 10

使用Len
 你可以使用Len(string)函数来确定文本的串的长度:

<%
IntString = "This is a Simple Sentence."
IntStringLength = Len(IntString)
Response.Write "There are " & IntStringLength & " characters (including spaces) in the sentence shown below:"
Response.Write "" & IntString & ""
%>

如果你想知道Len是如何手动工作,你可以想想你要求用户输入他们的五位数字代码或者三位PIN的形式。使用Len,你效验是否输入了足够的数字。

使用Trim
Trimming 串是你想要在开始就获得的东西。很多时候,一个串在开始或者结束的时候有一个额外的空间,如果你不平衡它,你可能会担心浪费时间到这些变量上。

<% strName = Request.Form("Name")
strCheckName = "Amy Cowen"
If strName = strCheckName THEN
Response.Write "Success! The names matched."
Else
Response.Write "Sorry. The names do not match."
End if
%>

如果strName的值是 " Amy Cowen",因为那个是我怎样将它输入到形式box中,然后测试两个变量是否一样,结果不是,因为 "Amy Cowen" 不是" Amy Cowen."

同样地,如果你将Name输入到URL中:

<% Response.Write " & objRec("Name") & "">Your Site" %>

如果Name中的记录的任何部分有额外的空间,你将迅速的执行错误问题。

你可以修正一整个串后者在左边或者右边执行进程:

<% strComments = Request.Form("Comments")
strComments = Trim(strComments)
%>

假定用户已经输入::

" I am having problems installing the software I downloaded. "

上面的修整语句将会打散额外的空间,只留下下面的内容:

"I am having problems installing the software I downloaded."

现在,回到我们的 " Amy Cowen" 例子,如果我添加了下面的脚本,我们就会成功:

strName = Trim(strName)

在右边修整, 使用Rtrim(string). 在左边修整, 使用Ltrim(string).

转换
当你开始使用和VB差不多的语言的时候,你会犯一些简单的错误,比如比较整型的512和串512。如果你认识到前一个512和后一个512是不一样的,你可以想想为什么给出的脚本不能正常工作。


假想一下,你传送一个文件的ID到ASP脚本中,使用Request.QueryString,你会确定这个文件的ID就是用户想要编辑的。你需要从数据库中为记录输入一些信息并将它们显示在屏幕上。数据库中的ID和整型差不多,特别地,如果你在那个区域使用了AutoNumber性能。你输入的ID事实上是一个串。因此,这两个永远不可能匹配除非你转换成相同的类型。

Request.QueryString:
使用FileSystemObject, 你可以测试一个文本—比如, *.html, *.asp, *.inc., *.gif—或者目录的存在。如果文件存在,你可以想要一连串的时间发生。如果文件不存在,你可能需要其他的事件发生,使用下面的代码: <%
    sPath="/profiles/" & strFileName & ".asp"
    sFile=Server.MapPath(sPath)
    Set fe=Server.CreateObject("scripting.FileSystemObject")
    if fe.FileExists(sFile) THEN
      'do something
      Response.Write "Yeah! I found it!."
      Response.Write "You can access that file by "
      Response.Write "<A HREF=""" & sPath & """>Clicking Here</A>."
    else
      'do something
      Response.Write "Sorry. The requested file does not exist."
    end if
%>

为了简单的测试你这个文件,添加脚本到最上面:

    strFileName = "name"
    ' First assign the name of a file you have to this variable.
' strFileName holds just the name, not the extension or the path.
    ' Make sure you change the path for sPath to the virtual directory your file is in
    ' Run the script.
    ' Then come back and change the strFileName variable to the name of a file
    ' you do NOT have.
  ' Run the script.

改变被输入的代码为一个整型是非常容易的并且可以让你以后的工作变得容易一点,样本的格式是这样的:

<%
dim intUserID intUserID = Request.QueryString("userID")
intUserID = CInt(intUserID)
' intUserID is now an integer.
%>

<

 

 

 
上一篇:ASP进阶学习必经之认识数学函数11种  下一篇:ASP六大对象介绍