Subject: get the text and not the value from a selectBox
Posted By: recipiente Post Date: 3/6/2007 1:13:35 AM
Hi, I have the following code which works fine...
Request.Form.Key() that gets the name of the object, then
Request.Form.Item() that gets the value of the selected option, but I need to get the Text also and don't know how to do it.



Reply By: dparsons Reply Date: 3/6/2007 7:36:48 AM
Request.Form("fieldname") will return the value.

===========================================================
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
===========================================================
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470131470.html
===========================================================
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
===========================================================
Reply By: recipiente Reply Date: 3/8/2007 7:12:09 AM
I'm sorry, I guess i didn't explained very well.

have this problem that I need to get the text out of a select box in classic ASP but haven't found how and I did search it over the net but nothing, let me explain it with an example:

if I have...

<select name="selOpt1" id="selOpt1">
         <option value="0" selected="selected">Select One</option>
         <option value="1.00">First Option</option>
         <option value="2.00">Second Option</option>
         <option value="3.00">Third Option</option>
       </select>

if I use Request.Form.Key() I get the name of the object, in this example "selOpt1" and if I use Request.Form.Item() I get the value of the selected option, lets say "1.00" or "2.00" etc, but what if need the text from the selected option? lets say "First Option" or "Second Option" that's actually my problem, don't know how to do it.

another thing that I probably need to mention is that I'm using these code on a second page, that collects the values from a form on the first page.

thanx
Reply By: saurabhj Reply Date: 3/9/2007 6:04:42 AM
That is not possible. You have to set the name as value of that element

Reply By: dparsons Reply Date: 3/9/2007 8:35:29 AM
Unfortunately, saurabhj is correct, at least using the Request object.  You may be able to get this information using a client side javascript but the request object is only going to get you the value.

In .NET doing what you want is not only possible, it is extremely easy.

Dim s1, s2 as String
s1 = selOpt.SelectedItem.Value
s2 = selOpt.SelectedItem.Text

===========================================================
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
===========================================================
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470131470.html
===========================================================
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
===========================================================
Reply By: woodyz Reply Date: 3/9/2007 10:20:04 AM
[It is puzzling as to why you need the text of the selected item.  What is your purpose for needing that?

If there is an actual need to have both the value and the text, you can concatenate the two with a delimiter and place them both in the value as you build the select box.  

<select name="selOpt1" id="selOpt1">
    <option value="0|None" selected="selected">Select One</option>
    <option value="1.00|First Option">First Option</option>
    <option value="2.00|Second Option">Second Option</option>
    <option value="3.00|Third Option">Third Option</option>
</select>


Then your code needs that handles the submit needs to parse the value to retrieve the values you need.

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
Reply By: recipiente Reply Date: 3/9/2007 11:13:32 AM
Hi again, first of all thank you saurabhj, dparsons & woodyz for your time. I'm creating a very basic form where the user select Yes or No with a select box and needed those selections on an email, so I'm using 2 files, one for the form and the other to create the email, the thing is that for every selection they made (Yes or No) it must have a value so that I can calculate the points they get, I know I can make some work around like the one that woodyz helpfully suggested, but wanted to make sure that there was no actual code for ASP that I can use to get the text.

Many thanks to all of you.

Reply By: woodyz Reply Date: 3/9/2007 11:59:21 AM
Conceptually, the value and the text are two different views of the same piece of data - the text is the human readable view and the value is the code readable view, and therefore it is not common to need to read the text of the select box in a request.  This is because in the typical case you can always determine what the text is by using the value as the key.

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems

Go to topic 52237

Return to index page 6
Return to index page 5
Return to index page 4
Return to index page 3
Return to index page 2
Return to index page 1