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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Drop Down Box

Status
Not open for further replies.

ceeleelewis

Programmer
Sep 26, 2002
45
US
I wonder is there a way for you to add conditions to a drop down box. I currently have a check list form that pre-populates checked fields based off the previous state of the page.(i.e.:if the user checked boxes 1, 3, 5 and submitted the form. When a user returned to that same form boxes 1, 3, 5 will be checked)

If a user a selects an option on a drop down box,and submits the form. The user should be able to see that same option selected when they return to that same form.

Again, thanks for the info and direction
 
Do it the same way, as your building the select box check the value of the form field:
Code:
Response.Write &quot;<select name=&quot;&quot;selColor&quot;&quot;>&quot;

Response.Write &quot;<option value=&quot;&quot;blue&quot;&quot;&quot;
If Request.Form(&quot;selColor&quot;) = &quot;blue&quot; Then response.Write &quot; selected&quot;
Response.Write &quot;> BLUE!!! </option>&quot;

'continue with color of your choise

Response.Write &quot;</select>&quot;

Of course this is a lot easier if they are in an array or recordset:
Code:
Dim myColors(3), i
myColors(0) = &quot;blue&quot;
myColors(1) = &quot;red&quot;
myColors(2) = &quot;green&quot;
myColors(3) = &quot;yellow&quot;

Response.Write &quot;<select name=&quot;&quot;selColor&quot;&quot;>&quot;
For i = 0 to UBound(myColors)
   Response.Write &quot;<option value=&quot;&quot;&quot;&myColor(i)&&quot;&quot;&quot;&quot;
   If Request.Form(&quot;selColor&quot;) = myColor(i) Then Response.Write &quot; selected&quot;
   Response.Write &quot;> &quot; & myColor(i) & &quot;!!! </option>&quot;
Next
Response.Write &quot;</select>&quot;

Hope that helps,
Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Tarwn,

Sorry about the mishap. i should've mention that I had a recordset that I wanted to check against. I was also psuedo-coding this.. am I on the right path? Or is your example sufficient enough for this issue..thanks for the insight...below is my psuedo-code...

<select name= &quot;CommandCenterGroup&quot;>

<%if objRS(&quot;cmdCntrgrp&quot;)=&quot;Adel&quot;%>
then <option selected>Adel</option>
<%elseif objRS(&quot;cmdCntrgrp&quot;)=&quot;Becky&quot;%>
then <option selected>Becky</option>
<%elseif objRS(&quot;cmdCntrgrp&quot;)=&quot;Stan&quot;%>
then <option selected>Stan</option>
<%elseif objRS(&quot;cmdCntrgrp&quot;)=&quot;Stacey&quot;%>
then <option selected>Stacey</option>

</select>


 
your then's are outside the <% %> tags ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
One problem, your only going to be outputting the option if it is the selected one, you need to have both cases:
Code:
<%
if objRS(&quot;cmdCntrgrp&quot;)=&quot;Adel&quot; then 
   Response.Write &quot;<option selected>Adel</option>&quot;
else
   Response.Write &quot;<option>Adel</option>&quot;
end if

if objRS(&quot;cmdCntrgrp&quot;)=&quot;Becky&quot; Then ...etc

You'll also notice I got rid of a lot of ASP start and end script tags, goping in and out of the script like that will cause a hit to the peformance, so try not to go in and out of the script on every line like you were above.

You may also want to lcase both sides just to be sure they match correctly:
Code:
if lcase(objRS(&quot;cmdCntrgrp&quot;))=lcase(&quot;Adel&quot;) then 
   Response.Write &quot;<option selected>Adel</option>&quot;
else
   Response.Write &quot;<option>Adel</option>&quot;
end if

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
also I'm a bit confused on why you are going through all that in your statements, the RS is going to have one value so why not jsut gove the value to
the select and be done with it?
<%
Dim opt
opt = objRS(&quot;cmdCntrgrp&quot;)
%>
<select name= &quot;CommandCenterGroup&quot;>
<option selected value=&quot;<%=opt%>&quot;><%=opt%></option>

if you're going to place the value of the RS into the select then jsut write it to the option also without making all the running around back and forth in a if statement to get what it is. ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
Well yes, that would work too :)

Course it means you have the value listed twice in your select box, but nothing wrong with that.

-Eli ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
I know. :( just got done writing something where I wanted a value also. guess I got into the swing of it. :) ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
Hello guys,


onpnt...


I tried your solution..but it seems like I must be missing something here.When I come back to my selected form, I don't see the same name I previously selected..

i.e.: I select &quot;Becky&quot; when I'm in the form for the first time. When I come back to the form, I see &quot;Stan&quot; in the drop down box.

The following is what I typed.. (pretty much the same thing you gave me)

<%
Dim opt
opt = objRS(&quot;cmdCntrgrp&quot;)
%>
<select name=&quot;CommandCenterGroup&quot;>
<option selected value=&quot;<%=opt%>&quot;><%=opt%>Becky</option>
<option selected value=&quot;<%=opt%>&quot;><%=opt%>Adel</option>
<option selected value=&quot;<%=opt%>&quot;><%=opt%>Stan</option>
</select>

I cleaned out the values (names) in the db field(objRS(&quot;cmdCntrgrp&quot;)) because the options in my drop down box looked like the following....
&quot;BeckyBecky&quot;| &quot;BeckyAdel&quot;|&quot;BeckyStan&quot;

But I wonder, how can get the value selected to show without having to see two values for a selected option?

Also, should I go inside of my db and change the field to either validate the fields or trim the field so its represented as one value in the db?
 
I apologize, the way I had suggested was compeltely the wrong way (but may work) but would be out of the ordinary
Tarwn had it correct in how to format things.

<%
if objRS(&quot;cmdCntrgrp&quot;)=&quot;Adel&quot; then
Response.Write &quot;<option selected>Adel</option>&quot;
else
Response.Write &quot;<option>Adel</option>&quot;
end if


how are you populating the select initially?

---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
how are you populating the select initially?


I don't know if I answering this right.. but Initially, I had this set up as a textbox that just displayed the recordset object. This is good and simple in theory but I wanted to have more control of the user input. That's were the drop down box comes into play....

I really didn't have anything populating the select initally because I under the impression that this was quite similar to a regular drop down box, with added conditions... (which I tried in psudeo code)

<select name= &quot;CommandCenterGroup&quot;>
<%if objRS(&quot;cmdCntrgrp&quot;)=&quot;Adel&quot;%>
 then <option selected>Adel</option>
<%elseif objRS(&quot;cmdCntrgrp&quot;)=&quot;Becky&quot;%>
 then <option selected>Becky</option>
<%elseif objRS(&quot;cmdCntrgrp&quot;)=&quot;Stan&quot;%>
 then <option selected>Stan</option>
<%elseif objRS(&quot;cmdCntrgrp&quot;)=&quot;Stacey&quot;%>
 then <option selected>Stacey</option>
</select>

Hope I explained that right...


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top