View Single Post
  #2 (permalink)  
Old March 14th, 2009, 01:38 PM
philip_cole philip_cole is offline
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default Use a Dictionary to show and retrieve values

Hi Ashok,
There isn't a way to get the text value they selected just by using Request.Form - you only ever get back the value part (you may be thinking of how ASP.NET allows you to use it, but that does it by parsing the html code in the file - nothing to do with request data).

You will have to keep a list of the text and values you want to show within the actual asp code.
A good way to do this is using an ASP Dictionary object, which is like an array, but stored as custom keys/values. See an introduction at http://www.w3schools.com/asp/asp_ref_dictionary.asp

Here is an example page which uses a Dictionary to print and then retrieve the text and value they have selected

vb Code:
<%

Dim myDic, dKeys, text, value

' create a dictionary object
Set myDic = Server.CreateObject("Scripting.Dictionary")
' add each value/text pair
myDic.Add "1", "ashok"
myDic.Add "2", "amit"
myDic.Add "3", "Sumil"

' store the list of keys (select values)
dKeys = myDic.Keys()

If Request.Form("checkDropDown") <> "" Then
  ' get the value selected and use dictionary to get text
  value = Request.Form("checkDropDown")
  text = myDic(value)
  ' add to database here
End If
%>
<html>
<head>
</head>

<body>
<form action="default.asp" method="post">

<select name="checkDropDown">
<%
' loop through dictionary, printing an option containing each key/value
Dim optValue, optText
For i = 0 To myDic.Count - 1
    optValue = dKeys(i)
    optText = myDic(optValue)
    Response.Write "<option value=""" & optValue & """>" & optText & "</option>"
Next
%>
</select>

<input type="submit" value="Go" />

</form>

<%
' For testing only
If Not IsEmpty(text) Then
    Response.Write text & " / " & value
End If
%>
</body>
</html>

You basically create the dictionary at the top of the page each time. If they have submitted the form, it will retrieve the selected value from the Request, then use the Dictionary to find the corresponding text. In the same way, it builds up the select html using the keys and values from the Dictionary.

HTH
Phil

Last edited by philip_cole; March 14th, 2009 at 01:39 PM.. Reason: Typing errors
Reply With Quote
The Following User Says Thank You to philip_cole For This Useful Post:
beetle_jaipur (March 28th, 2009)