Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

default value in listbox as variable

Status
Not open for further replies.

typo291

Technical User
May 5, 2003
2
GB
i am trying to display a default value in a listbox(html select) based on a variable taken from a cookie. The only way I know how to use a default value is the option selected tag, but that isn't going to work easily. Does anyone know if its possible to base the selected option on a variable, and if so, how?

Vikki
 
yes...sorry should have pointed that out
 
You need to assign the variable to a cookie, I assume you have done this already

<% var1 = request.cookies(&quot;cookiename&quot;) %>

now you can generate the &quot;SELECTED&quot; part of the option based on the variable:

<select name=&quot;name&quot;>
<option value=&quot;value1&quot;<% If var1 = &quot;value1&quot; then response.write(&quot; selected&quot;) %>>value1</option>
<option value=&quot;value2&quot;<% If var1 = &quot;value2&quot; then response.write(&quot; selected&quot;) %>>value2</option>
<option value=&quot;value3&quot;<% If var1 = &quot;value3&quot; then response.write(&quot; selected&quot;) %>>value3</option>
</select>

Since the ASP is discerned first, what will be written to the browser will be based on your variable. So if your cookie contains the text &quot;value2&quot; the generated HTML will be:

<select name=&quot;name&quot;>
<option value=&quot;value1>value1</option>
<option value=&quot;value2&quot; selected>value2</option>
<option value=&quot;value3&quot;>value3</option>
</select>

Hope this helps,

BDC.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top