|
 |
asp_web_howto thread: retaining input values after refreshing the page
Message #1 by "wasfie" <pesfie@w...> on Tue, 3 Sep 2002 07:57:29
|
|
my problem is simple. i have two combo boxes, the one populating the other
from a database based on selection in the first. to do this updating of
the dropdown i need to refresh the page by posting it to itself. when i do
this the other info is lossed. i also want to know if u can write the
input data on a form directly to a table below it without having to submit
to the db. i need to know urgently as it is for a project which is due
very soon
Message #2 by "Ken Schaefer" <ken@a...> on Tue, 3 Sep 2002 17:05:50 +1000
|
|
This is a repost of a previous answer that I gave to the same question a few
weeks ago:
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: "wasfie" <pesfie@w...>
Subject: [asp_web_howto] retaining input values after refreshing the page
: my problem is simple. i have two combo boxes, the one populating the other
: from a database based on selection in the first. to do this updating of
: the dropdown i need to refresh the page by posting it to itself. when i do
: this the other info is lossed. i also want to know if u can write the
: input data on a form directly to a table below it without having to submit
: to the db. i need to know urgently as it is for a project which is due
: very soon
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
 |