Introduction
This article tries to throw light on how we can add options to a ComboBox using client-side scripting. This can be useful in many situations where we do not want a form to be submitted each time we want to add something to a ComboBox. The article also explains how we can get the parent folder of the current ASP file. This is useful in situations where you need to get the full physical path of a file in your application directory, as for generating the connection strings for a DB access.
Using the code
Adding elements to a ComboBox using client side script
Here we create an instance of "OPTION", assign value to it and add it to the ComboBox. The CreateElement
method of document object creates an instance of an element for the "OPTION" tag.
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
The variable a
is used to track the number of items in the ComboBox. After adding the new element, we set the newly added item as the current selection.
Adding elements to a ComboBox in a different frame using client side script
The method of adding an item is same here except that we refer to the ComboBox in a different frame. The frame index is taken from 0 - n, where we have n frames in the parent window.
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
Generating client-side script using 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
%>
Here we have five text-boxes, buttons and corresponding ComboBoxes. The script for each button is generated dynamically. The ASP is embedded in the script code, so you do not have to handle the new lines individually.
<INPUT id="Text<%=i%>" type="text" name="Text<%=i%>">
<INPUT id="Button<%=i%>" type="button" value="Add<%=i%>" name="Button<%=i%>">
Finding the parent folder of current script
Here we will see two methods to get the parent folder of the current script. One instance where I found this useful was when I wanted to generate the connection string to connect to an Access database in a subfolder of the application directory.
Both methods use the ServerVariables
collection of the Request
method to get the virtual path of the running script. This value is then mapped to a physical path by using the MapPath
method of Server
object to get the physical path of the script. The first method locates the position of last '\' and gets the parent folder by removing the file name of the script.
phypath = Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))
slashpos = instrrev(phypath,"\")
getpath = left(phypath,slashpos-1)
The second method uses the FileSystemObject
object to get the parent folder.
phypath = Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))
Set fsobj = Server.CreateObject("Scripting.FileSystemObject")
Set fileobj = fsobj.GetFile(phypath)
Set getdir = fileobj.ParentFolder
Here we first create a File
object which represents the current file. The ParentFolder
method of the File
object then helps us to acquire the parent folder of the file. To get the above method working, you will require the Scripting Runtime Library [scrrun.dll] to be properly installed.