Click here to Skip to main content
14,028,207 members
Click here to Skip to main content
Add your own
alternative version

Stats

195K views
1K downloads
63 bookmarked
Posted 21 Dec 2002
Licenced CPOL

Dynamically populating ComboBoxes using ASP/VBScript and Detecting the parent folder of the current script

, 21 Dec 2002
Rate this:
Please Sign up or sign in to vote.
An article on how to dynamically add strings to any number of drop downs using VBScript and ASP. Also explains how to get the parent folder of an ASP file.

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.

'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)		

The second method uses the FileSystemObject object to get the parent folder.

'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

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Smitha Nishant
The Code Project
United States United States
Smitha is a software developer, and has been in the industry since January 2000. She has experience in ASP.NET, C#, Windows Forms, Visual Basic, ASP, JavaScript, VBScript, and HTML. She has been with CodeProject since 2003 and currently works as Senior Editor.

In her free time, she tries out new dishes, reads a little, and puts together jigsaw puzzles. Originally from Trivandrum, Smitha currently lives with her husband and fellow CP'ian Nish [^] and her son Rohan in Columbus, Ohio.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
John Blenkinsop12-Feb-12 23:20
memberJohn Blenkinsop12-Feb-12 23:20 
GeneralExcellent work Pin
Brij7-Jan-09 21:12
mentorBrij7-Jan-09 21:12 
Generalwant 2 use ASP code in vbscript Pin
rumsraja26-May-04 19:45
sussrumsraja26-May-04 19:45 
GeneralASP Pin
suman_sunil5-Feb-04 4:23
membersuman_sunil5-Feb-04 4:23 
Generalthanks and offtopic q Pin
evilmousse24-Dec-03 7:35
memberevilmousse24-Dec-03 7:35 
Generalremoving options in combobox Pin
radhixz1-May-03 4:07
memberradhixz1-May-03 4:07 
Generalhaha Pin
xxrl17-Feb-03 19:01
memberxxrl17-Feb-03 19:01 
Generalgood work!!! Pin
pkravikkumar22-Jan-03 0:13
memberpkravikkumar22-Jan-03 0:13 
GeneralRe: good work!!! Pin
Smitha Nishant22-Jan-03 1:48
protectorSmitha Nishant22-Jan-03 1:48 
GeneralRe: good work!!! Pin
pkravikkumar23-Jan-03 0:12
memberpkravikkumar23-Jan-03 0:12 
GeneralWonderful article, beautiful girl! Pin
xxrl2-Jan-03 15:18
memberxxrl2-Jan-03 15:18 
Generalrealy useful Pin
Jaygiri27-Dec-02 9:34
memberJaygiri27-Dec-02 9:34 
GeneralRe: realy useful Pin
Nish Nishant27-Dec-02 9:55
sitebuilderNish Nishant27-Dec-02 9:55 
GeneralRe: realy useful Pin
Jaygiri30-Dec-02 4:28
memberJaygiri30-Dec-02 4:28 
GeneralIE dependency Pin
Nish Nishant23-Dec-02 4:10
sitebuilderNish Nishant23-Dec-02 4:10 
GeneralRe: IE dependency Pin
Smitha Nishant23-Dec-02 4:18
protectorSmitha Nishant23-Dec-02 4:18 
GeneralRe: IE dependency Pin
Nish Nishant23-Dec-02 6:26
sitebuilderNish Nishant23-Dec-02 6:26 
GeneralCongrats ! Pin
Kannan Kalyanaraman23-Dec-02 2:41
memberKannan Kalyanaraman23-Dec-02 2:41 
GeneralRe: Congrats ! Pin
Nish Nishant23-Dec-02 3:26
sitebuilderNish Nishant23-Dec-02 3:26 
GeneralRe: Congrats ! Pin
Smitha Nishant23-Dec-02 3:30
protectorSmitha Nishant23-Dec-02 3:30 
GeneralRe: Congrats ! Pin
Zeeshan Mehmood15-Feb-03 6:57
memberZeeshan Mehmood15-Feb-03 6:57 
GeneralOld style ASP Pin
Nish Nishant22-Dec-02 16:35
sitebuilderNish Nishant22-Dec-02 16:35 
GeneralRe: Old style ASP Pin
James T. Johnson22-Dec-02 16:38
memberJames T. Johnson22-Dec-02 16:38 
GeneralRe: Old style ASP Pin
Nish Nishant22-Dec-02 16:42
sitebuilderNish Nishant22-Dec-02 16:42 
GeneralRe: Old style ASP Pin
Paul Ingles23-Dec-02 1:11
memberPaul Ingles23-Dec-02 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190419.4 | Last Updated 22 Dec 2002
Article Copyright 2002 by Smitha Nishant
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid