|
 |
asp_web_howto thread: Dropdown listbox help needed
Message #1 by "Eric McCraw" <eric.mccraw@n...> on Sat, 20 Jul 2002 01:44:59
|
|
I have a user table which I populate from a web form. One field in this
table is the user's job title. This job title is meant to be chosen from a
list of available job titles. When I add a new user, this is no problem as
I populate the dropdown list box with:
<select size="1" name="JobID">
<option selected>Select Job</option>
<%
set os= Server.CreateObject("ADODB.Recordset")
mySQL = "SELECT JobID, Job FROM TJob ORDER BY Job"
os.ActiveConnection = conn
os.Open mySQL
if not os.EOF then
os.movefirst
end if
do while not os.EOF
response.write "<option value="
response.write os("JobID")
response.write (">")
response.write os("Job")
response.write "</option>"
response.write "<br>"
os.movenext
loop
mySQL = ""
os.close
set os = nothing
%> </option>
</select>
However I need to allow my users to update/edit user data, including this
job title. I am able to populate the text boxes on the form with no
difficulty. However, how do I set the selected option of the dropdown list
box = to the correct jobID/job in my drop down list box?? very puzzled.
Message #2 by "Ken Schaefer" <ken@a...> on Mon, 22 Jul 2002 13:37:44 +1000
|
|
There are several ways to do this. My strongest suggestion is to encapulate
the code inside a routine, and pass the necessary parameters (I have pasted
a routine at the bottom of my post that will do this for you).
The two main techniques are:
a) loop through the records and check the value of each record against what
the user's value is. If it is the same then response.write the necessary
HTML (ie "selected") into the <option> tag.
b) loop through all the records creating a string that contains all the HTML
for the <select> box, and then do a replace on the <option> tag that matches
the user's value, replacing it with <option value="" selected>. DO NOT use
this last technique if you have a large number of available options because
concatenation large strings in VBScript sucks in a major way.
Here is a demonstration:
<%
' Get User's job ID
intUserJobID = objUserRS("JobID")
' Get all jobIDs/Titles
arrJobTypes = objJobsRS.GetRows
' see www.adopenstatic.com/resources/code/objdispose.asp
Call objDispose(objJobRS, True, True)
Call objDispose(objUserRS, True, True)
'
'
'
' Now create a large string
strCboJobTitles = "<select name=""cboJobTitles"">" & vbCrLf
If isArray(arrJobTypes) then
For i = 0 to UBound(arrJobTypes, 2)
strCboJobTitles = strCboJobTitles & _
"<option value=""" & arrJobTypes(0,i) & """>" & arrJobTypes(1,i)
& "</option>" & vbCrLf
Next
End if
strCboJobTitles = strCboJobTitles & "</select>" & vbCrLf
' Now insert appropriate selected text
strCboJobTitles = Replace( _
strCboJobTitles, _
"<option value=""" & intUserJobID & """>", _
"<option value=""" & intUserJobID & """
selected>")
' Now write out HTML
Response.Write(strCboJobTitles)
%>
Here is a generic routine that you could also use (watch for wrapping). The
argument list is in the comments at the top.
'------------------------------------------------------------
' WriteFormSelectList
'------------------------------------------------------------
' Returns HTML for a <select> element
' Accepts strName as string
' Accepts strID as string
' Accepts strHTMLAttributes as string - literal text for any other HTML
attributes
' Accepts form option elements as array
' Accepts array format as integer: 0 = rows/cols, 1 = cols/rows (eg from
getRows)
' Accepts optional text for first <option></option> tag
' Accepts strSelectedValue as option to be selected
'------------------------------------------------------------
Function WriteFormSelectList( _
ByVal strName, _
ByVal strID, _
ByVal strHTMLAttributes, _
ByVal arrOptions, _
ByVal intArrayFormat, _
ByVal strFirstOption, _
ByVal strSelectedValue _
)
Dim i ' array 1st dimension counter
If not isArray(arrOptions) then
Exit Function
End If
WriteFormSelectList = "<select name=""" & strName & """ ID=""" & strID &
""""
If Len(strHTMLAttributes & "") > 0 then
WriteFormSelectList = WriteFormSelectList & " " & strHTMLAttributes & """"
End If
WriteFormSelectList = WriteFormSelectList & ">" & vbCrLf
If Len(strFirstOption) > 0 then
WriteFormSelectList = WriteFormSelectList & strFirstOption & vbCrLf
End If
If intArrayFormat = 0 then
For i = 0 to Ubound(arrOptions, 1)
WriteFormSelectList = WriteFormSelectList & "<option value=""" &
arrOptions(i,0) & """>" & arrOptions(i,1) & "</option>" & vbCrLf
Next
Else
For i = 0 to Ubound(arrOptions, 2)
WriteFormSelectList = WriteFormSelectList & "<option value=""" &
arrOptions(0,i) & """>" & arrOptions(1,i) & "</option>" & vbCrLf
Next
End If
WriteFormSelectList = WriteFormSelectList & "</select>" & vbCrLf
If Len(strSelectedValue & "") > 0 then
WriteFormSelectList = Replace(WriteFormSelectList, "value=""" &
strSelectedValue & """>", "value=""" & strSelectedValue & """ selected>")
End If
End Function
'------------------------------------------------------------
' --- WriteFormSelectList
'------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Eric McCraw" <eric.mccraw@n...>
Subject: [asp_web_howto] Dropdown listbox help needed
: I have a user table which I populate from a web form. One field in this
: table is the user's job title. This job title is meant to be chosen from a
: list of available job titles. When I add a new user, this is no problem as
: I populate the dropdown list box with:
:
: <select size="1" name="JobID">
: <option selected>Select Job</option>
: <%
: set os= Server.CreateObject("ADODB.Recordset")
: mySQL = "SELECT JobID, Job FROM TJob ORDER BY Job"
: os.ActiveConnection = conn
: os.Open mySQL
: if not os.EOF then
: os.movefirst
: end if
: do while not os.EOF
: response.write "<option value="
: response.write os("JobID")
: response.write (">")
: response.write os("Job")
: response.write "</option>"
: response.write "<br>"
: os.movenext
: loop
: mySQL = ""
: os.close
: set os = nothing
: %> </option>
: </select>
:
:
: However I need to allow my users to update/edit user data, including this
: job title. I am able to populate the text boxes on the form with no
: difficulty. However, how do I set the selected option of the dropdown list
: box = to the correct jobID/job in my drop down list box?? very puzzled.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
 |