65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ASP/VBScript 动态填充 ComboBox 并检测当前脚本的父文件夹

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.97/5 (63投票s)

2002年12月22日

CPOL

2分钟阅读

viewsIcon

217593

downloadIcon

1056

一篇文章,介绍如何使用 VBScript 和 ASP 动态地向任意数量的下拉列表中添加字符串。同时还解释了如何获取 ASP 文件的父文件夹。

引言

本文旨在阐述如何使用客户端脚本向 ComboBox 添加选项。这在许多情况下都很有用,例如,我们不希望每次向 ComboBox 添加内容时都提交表单。文章还解释了如何获取当前 ASP 文件的父文件夹。这在需要获取应用程序目录中文件的完整物理路径时很有用,例如生成数据库访问的连接字符串。

使用代码

使用客户端脚本向 ComboBox 添加元素

在这里,我们创建一个 "OPTION" 实例,为其赋值,然后将其添加到 ComboBox 中。document 对象的 CreateElement 方法创建 "OPTION" 标签的元素实例。

Dim a
a = 1

Sub Button1_OnClick
    If Text1.value <> "" Then
        Set objOpt = document.CreateElement("OPTION")
        objOpt.Value = a				
        objOpt.Text = Text1.value
        Select1.add objOpt
        Set objOpt = Nothing
        Select1.selectedIndex = a
        a = a + 1		
    End If
End Sub 

变量 a 用于跟踪 ComboBox 中的项目数量。添加新元素后,我们将新添加的项目设置为当前选择。

使用客户端脚本向不同框架中的 ComboBox 添加元素

添加项目的方法与之前相同,只是我们引用了不同框架中的 ComboBox。框架索引从 0 到 n,其中 n 是父窗口中的框架数量。

Set objOpt = document.CreateElement("OPTION")
objOpt.Value = a				
objOpt.Text = Text1.value
window.parent.frames(0).Select1.add objOpt
Set objOpt = Nothing
window.parent.frames(0).Select1.selectedIndex=a
a = a+1		

使用 ASP 生成客户端脚本

<%
    Dim i
    For i= 1 to 5
%>

Dim a<%=i%>
a<%=i%> = 1

Sub Button<%=i%>_onclick
    if Text<%=i%>.value <> "" Then
        Set objOpt = document.CreateElement("OPTION")
        objOpt.Value = a<%=i%>				
        objOpt.Text = Text<%=i%>.value
        window.parent.frames(0).Select<%=i%>.add objOpt
        Set objOpt = Nothing
        window.parent.frames(0).Select<%=i%>.selectedIndex=a<%=i%>
        a<%=i%> = a<%=i%> + 1
    End If
End Sub 

<%
    Next
%>		
		

这里有五个文本框、按钮和相应的 ComboBox。每个按钮的脚本都是动态生成的。ASP 嵌入在脚本代码中,因此您不必单独处理换行符。

<INPUT id="Text<%=i%>" type="text" name="Text<%=i%>"> 
<INPUT id="Button<%=i%>" type="button" value="Add<%=i%>" name="Button<%=i%>">

查找当前脚本的父文件夹

在这里,我们将看到两种获取当前脚本父文件夹的方法。我发现这很有用的一个例子是,当我想要生成连接到应用程序目录子文件夹中的 Access 数据库的连接字符串时。

两种方法都使用 Request 方法的 ServerVariables 集合来获取正在运行的脚本的虚拟路径。然后使用 Server 对象的 MapPath 方法将此值映射到物理路径,以获取脚本的物理路径。第一种方法定位最后一个 '\' 的位置,并通过删除脚本的文件名来获取父文件夹。

'Convert the virtual path of the current file to physical path		
phypath = Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))

'locate position of \ from the end
slashpos = instrrev(phypath,"\")	

'separate the folder name
getpath = left(phypath,slashpos-1)		

第二种方法使用 FileSystemObject 对象来获取父文件夹。

'Convert the virtual path of the current file to physical path		
phypath = Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))

'Create a FileSystemObject instance
Set fsobj = Server.CreateObject("Scripting.FileSystemObject")

'Create a File object representing the file, specified by phypath
Set fileobj = fsobj.GetFile(phypath)

'get the parent folder of fileobj
Set getdir = fileobj.ParentFolder
		

在这里,我们首先创建一个 File 对象,该对象表示当前文件。然后,File 对象的 ParentFolder 方法帮助我们获取文件的父文件夹。要使上述方法正常工作,您需要正确安装脚本运行时库 [scrrun.dll]。

© . All rights reserved.