Solved

Drop-down value selected

Posted on 2001-08-14
24
329 Views
Last Modified: 2012-08-14
I'm developing a few pages in ASP using HTML and VBscript.  Currently I am working on something where the user types in search criteria and hits a submit button, then it sends the info to another page and lists the results.  When they go back to the search page, I want all the criteria they entered to still be in the boxes.  I have it working with the text boxes but the drop-downs keep coming up with the default value instead of what they selected.

Example: in a drop-down list of 1-9, if they choose '5' and then hit submit, when they go back to that page I want the drop-down to have the '5' selected still instead of the default.  I am passing the values through - in the URL '5' would still be selected, it just doesn't show in the drop-down.

Hope this makes sense - can someone help?  Thanks
0
Comment
Question by:ags00
  • 6
  • 5
  • 5
  • +4
24 Comments
 
LVL 7

Expert Comment

by:John844
ID: 6384265
you will have to add code where you generate the drop down list

add something like

dim intValue
if intValue = "" then
    intValue = 0
else
    intValue = cint(request("Value"))
end if

<option value="1"<%if intValue = 1 then response.write "SELECTED" %> >test1</option>
<option value="2"<%if intValue = 2 then response.write "SELECTED" %> >test2</option>
0
 
LVL 20

Expert Comment

by:Silvers5
ID: 6384278
store the value of the drop down in a session variable where you post to..

session("etc") = request("etc")

now in the main page you type

<select name=etc>
<option value=bla1 <%if Session("etc")="bla1" then response.write "selected"%> >bla1
<option value=bla2 <%if Session("etc")="bla2" then response.write "selected"%> >bla2
<option value=bla3 <%if Session("etc")="bla3" then response.write "selected"%> >bla3
</select>

rgrds

0
 
LVL 7

Expert Comment

by:John844
ID: 6384313
the key word in drop downs is SELECTED.  If you add that keyword to the option, then it will show up as selected.  You should only select one item or it will cause funny results in netscape.

Another option would be to just add a new item to the top of the list like
<select name=someName>
<%
strValue = request("someName")
if not strValue = "" then
    response.write "<OPTION value=""" & strValue & """>" & strValue & "</OPTION>"
end if
%>
the rest of your options.
</SELECT>

the second option would show the selected item twice in the list like:
5 (selected item)
1
2
3
4
5
...

using the first option would only show the itme list like it was originally
1
2
3
4
5 (selected item)
...
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6385229
I suggest using Silvers's method.

hongjun
0
 
LVL 7

Expert Comment

by:John844
ID: 6385259
hongjun, why use a session variable to do something that a page level variable will do?  That hurts scalability if that is a consideration.
0
 

Author Comment

by:ags00
ID: 6385698
Thanks guys, I will try this soon I hope - I am caught up in an "urgent" project right now and had to put this on the back burner for awhile - will let you know :-)
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6387348
Just wanted to pick up on one thing you said: "When they go back to the search page". You will have a link on the results page that will take them back to the search page yes? Otherwise if you're relying on the user hitting the back button, then none of the above suggestions would work, because the values you want won't be in the querystring.

Anyway, assume you do have a link back to the search page, first thing you're going to need to do is load all the search criteria into the href for the "back to search page" button.

And then you're in a position to use Silver's solution, although I'd personally wrap it all up in a function.

For example.

<select name="txtSomething">
  <option value="1" <%=SelectThis("1", strSomething)%>>One</option>
  <option value="2" <%=SelectThis("2", strSomething)%>>Two</option>
</select>

<%
Function SelectThis(ThisOption, ChosenOption)
  If strComp(ThisOption, ChosenOption) = 1 Then _
    SelectThis = "SELECTED"
End Function
%>

And that'll do it.

Enjoy,

Daniel
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6388052
Except, you're going to want to check if strComp(value1, value2) = 0! Doh. That's when they equal. 1 is greater than, -1 is less than.

Dan.
0
 
LVL 7

Expert Comment

by:John844
ID: 6388480
AlfaNoMore, it is common practice to post all information as comments.  When you post your information as an answer, the question is removed from the active list of questions that users normally view.  The person can still accept your comments as the answer that they choose to use.

By the way, everybody starts out by doing this.
Enjoy EE
John
0
 
LVL 20

Expert Comment

by:Silvers5
ID: 6392112
John844 the reason I used sessions is that posting might be made to another page.. so the value won't be passed when going back unless you store it in a session variable , also some caching suppression code should be added so the browser won't take from its own cache:

Caching is hard to manipulate, (is it o bug or not) you need to add several lines on the client and
server side (although this won't eleminate the problem 100% but will minimise it a lot)
<%..
Response.expires= 0
Response.Expires=-1
Response.Expiresabsolute = Now() - 1
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
%>
<meta http-equiv="PRAGMA" value="NO-CACHE">
<meta http-equiv="Expires" content="Mon, 01 Jan 1990 12:00:00 GMT">
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6392120
Silvers5, exactly :)
Just too tired to explain. Will not be participating in any questions anymore.

hongjun
0
 
LVL 7

Expert Comment

by:John844
ID: 6393119
got you now.  I post to the same page 99% of the time, so I have not needed to deal with that.
John
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Expert Comment

by:Silvers5
ID: 6395942
well.. not everybody does this.. although I also post to the same page most of the time.. but there are times where you need to post to other pages..
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6397585
haha.
I just broke my promise. Just participate on some questions. Those that don't require much coding.

hongjun
0
 
LVL 11

Expert Comment

by:mouatts
ID: 6403356
Just a minor point to silver5. Using both the expiry and no-cache is mutually exclusive.

Using expires causes the item to be placed in the cache but then discarded when next used.

Using no-cache causes the item not to be placed in the cache at all.

As the browser can't do both and the HTTP standard doesn't say what it should do under these circumsantces its a toss up what the browser will actually do.

It might seem to have little significance but all the immediate expires have the tendancy to flush out good stuff from proxy server caches.

Steve
0
 
LVL 20

Expert Comment

by:Silvers5
ID: 6405181
mouatts
Cache suppression is hard to perform.. due to browsers inconformability to the standarts.. it is known that each version of IE or Netscape will behave differently for these headers .. that's why all should be included to increase the suppression rate..


AlfaNoMore , welcome to EE
usually (nettiquette) we don't lock the question by posting our solutions as answers, we post them as comments and the asker can choose to accept a comment as answer...when you lock the question you remove it from the answer waiting list, thus minimising the askers chances to get a better solution from other experts
Besides @ the bottom of the page you have tips on comments and answers so read them.
rgrds




0
 
LVL 11

Expert Comment

by:mouatts
ID: 6405692
Silver5

If you check out the PAQs in the CGI area you will find a lot of work was done in this area about 18 months ago.

Basically if you send the correct headers they will work in both IE and Netscape. However Netscape caching doesn't work correctly for anything other than the HTML page. Send both and it was discovered that one always discards the no-cache and the other always discards the expires (I can't remember which was which though).

The belief that you need to send expires and no-cache goes back to HTTP 0.9 days when some servers didn't support no-cache. For some reason this misconception continues to be expounded today.

In your example however you have a couple of problems.

Firstly HTTP-EQUIV meta tags are not interpreted by the client but are instructions to the server to issue those HTTP Headers. However this only occurs on mime-types that have head parsing. ASP is not one of these so the Meta tags here are defunct.

Secondly the expires method only works if a Date header is also sent as the date comparision is against this and not the system date on the server. However the date header is rarely issued by any server so when you send just the expires header the whole of the HTTP header no longer conforms to the HTTP standards and therefore different clients may behave differently.

Steve
0
 

Expert Comment

by:santhoshgivesuanswers
ID: 6409252
hi
all you have to do is use hidden fields and on change of every combo assign the combo value to the hidden fields
then call the function fillcomboselected or if the page is loaded for the first time then the hidden field value will be null so call fillcombo. simple i suppose

 'To Fill A ComboBox
  Function FillCombo(rs,field1,field2)
     dim Vfield1
     dim Vfield2
     do while not rs.eof
     Vfield1=field1
     Vfield2=field2
     Response.Write("<option value='"&Vfield1&"'>"&Vfield2&"</option>")
      rs.MoveNext
      Loop                  
  End Function

2 function

  Function FillComboSelected(rs,field1,field2,value)
               dim Vfield1
               dim Vfield2
                    do while not rs.eof
                         Vfield1=field1
                         Vfield2=field2
                         Response.Write "<option value='"&Vfield1&"'"
                         If (vfield1=value) then
                              Response.Write "selected"
                         End if
                         Response.Write ">"&Vfield2&"</option>"
                         rs.MoveNext
                    loop                  
          End Function


here the value will contain the hidden fields value

rgds
santhosh
0
 

Author Comment

by:ags00
ID: 6430075
Sorry alfa, still need help - just got back from honeymoon and I finally get a chance to look at this
0
 

Author Comment

by:ags00
ID: 6430076
Sorry alfa, I just got back from my honeymoon and finally have a chance to look at this stuff.
0
 

Author Comment

by:ags00
ID: 6430113
John 844/Silvers:

The drop-down values are coming from a recordset - it's set up like

<SELECT name=SEL_STG>
     <OPTION></OPTION>
        <%DO UNTIL STG.EOF%>
     <OPTION VALUE=<%Response.Write (STG("STAGE"))%>
          <%Response.Write (STG("STAGE"))%>
          </OPTION>
          <%STG.MOVENEXT
          LOOP%>
          </SELECT>

st is the value that's being passed from the link on the detail page.  And yes, there is a link back to the page, I'm not counting on them clicking the back button.  There are actually about 10 values being passed back and forth.

I used your suggestions and set this up right after the first Response.Write (STG("STAGE")) :

<%if rtrim(st) = Response.write (STG("STAGE")) then%>
          SELECTED
     <%end if%>>

And it didn't work.  Do I have to specifically list each option separately like in your examples?  Or is there a different way to use the SELECTED function with values coming from a function?

Hope this makes sense - sorry for my absence.

Thanks!
0
 
LVL 20

Expert Comment

by:Silvers5
ID: 6430816
do it like this:


<SELECT name=SEL_STG>
    <OPTION></OPTION>
       <%DO UNTIL STG.EOF%>
    <OPTION VALUE=<%Response.Write (STG("STAGE"))%> <%if Session("SEL_STG") = STG("STAGE") then response.write " Selected"%>>
         <%Response.Write (STG("STAGE"))%>
         </OPTION>
         <%STG.MOVENEXT
         LOOP%>
         </SELECT>


In previous post catches you would've been stored the request("SEL_STG") into a session called Session("SEL_STG")

Session("SEL_STG")  = request("SEL_STG")
0
 
LVL 7

Accepted Solution

by:
John844 earned 75 total points
ID: 6431755
give this a try.  Where you had

<%if rtrim(st) = Response.write (STG("STAGE")) then%>
         SELECTED
    <%end if%>>

change it to

<%if rtrim(cstr(st)) = cstr(STG("STAGE")) then
      response.write " SELECTED "
end if%>>
0
 

Author Comment

by:ags00
ID: 6685960
Sorry it took so long to give you your points.  It worked perfectly :-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now