943,685 Members | Top Members by Rank

Ad:
  • ASP Discussion Thread
  • Marked Solved
  • Views: 3151
  • ASP RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 22nd, 2008
0

Make an option stay selected

Expand Post »
I thought this was a simple task to do, but not.

I just want the option stay selected when a value from the session variable exists, AND it does after the form has been submitted. So why can't the option value stay selected as long as session is populated.

If I put a static value in the loop like this:
If RegID = 5 Then ...

Then the option stays selected at this post.


ASP Syntax (Toggle Plain Text)
  1. SQL = "SELECT * FROM tbregion ORDER BY region ASC"
  2.  
  3. Set RS = Server.CreateObject("ADODB.recordset")
  4. RS.Open SQL,Conn
  5.  
  6. Session("region") = Request.Form("regv")
  7. region = Session("region")
  8. %>
  9.  
  10. <form name="region" action="select_menu.asp" method="post">
  11. <select name="regv" onchange="region.submit()">
  12.  
  13. <option value="">Make a selection</option>
  14. <option value="">------------------</option>
  15.  
  16. <%
  17.  
  18. RegID = RS("regionID")
  19.  
  20. x=0
  21. Do While Not RS.EOF
  22.  
  23. If RegID = region Then
  24. For i = 1 To Region
  25. Response.Write("<option value='" & RS("regionID") & "' SELECTED>" & RS("region") & "</option>")
  26. x=x+1
  27. Next
  28.  
  29. Else
  30.  
  31. x=x -1
  32. For i = 1 To RegID
  33. Response.Write("<option value='" & RS("regionID") & "'>" & RS("region") & "</option>")
  34. Next
  35. End If
  36.  
  37.  
  38. RS.MoveNext
  39. Loop
  40. RS.Close
  41. Set RS = Nothing
  42. %>
  43.  
  44. </select>
  45. </form>
  46.  
  47. <%
  48. Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
  49. %>
Last edited by TobbeK; Feb 22nd, 2008 at 8:00 am.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Feb 22nd, 2008
0

Re: Make an option stay selected

Have you tried declaring these?

I have ran into some odd ball issues when comparing text field to integer.

Try setting both as integer then do your compare.
Reputation Points: 12
Solved Threads: 19
Posting Pro
techniner is offline Offline
521 posts
since May 2005
Feb 22nd, 2008
0

Re: Make an option stay selected

Been there, done that :-)
Does not help

What I tried to in my code is to loop from BOF until the values match and make that post SELECTED, and then keep on looping from that value to EOF.
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Feb 22nd, 2008
0

Re: Make an option stay selected

Now it works, with a small little bug.

If I have selected an option with (let's say value=20) then responded value is 19. So there is a miscalculation that takes the actual value and set it to -1.
I don't know where the bug is.

It is possible to set the session variable to +1 but I really like know where the error is.

ASP Syntax (Toggle Plain Text)
  1.  
  2. SQL = "SELECT * FROM tbregion ORDER BY region ASC"
  3.  
  4. Set RS = Server.CreateObject("ADODB.recordset")
  5. RS.Open SQL,Conn
  6.  
  7. Dim region
  8.  
  9.  
  10. %>
  11.  
  12. <form name="region" action="select_menu.asp" method="post">
  13. <select name="regv" onchange="region.submit()">
  14.  
  15. <option value="">Make a selection</option>
  16. <option value="">------------------</option>
  17.  
  18. <%
  19.  
  20. Session("region") = Request.Form("regv")
  21. region = Session("region")
  22. regionID = RS("regionID")
  23.  
  24.  
  25.  
  26. If region <> "" Then
  27. If region <> "0" Then
  28.  
  29.  
  30. Do Until Clng(regionID) = Clng(region)
  31.  
  32. Response.Write("<option value=""" & regionID & """SELECTED>" & RS("region") & "</option>")
  33. regionID = regionID + 1
  34. RS.MoveNext
  35. Loop
  36.  
  37.  
  38. Do Until RS.EOF
  39.  
  40. Response.Write("<option value=""" & regionID & """>" & RS("region") & "</option>")
  41. regionID = regionID + 1
  42. RS.MoveNext
  43. Loop
  44.  
  45. End If
  46. End If
  47.  
  48.  
  49. If region = "" OR region = "0" Then
  50. Do Until RS.EOF
  51. Response.Write("<option value=""" & regionID & """>" & RS("region") & "</option>")
  52. regionID = regionID + 1
  53. RS.MoveNext
  54. Loop
  55.  
  56. End If
  57. %>
  58.  
  59. </select>
  60. </form>
  61.  
  62. <%
  63. Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
  64. %>
Last edited by TobbeK; Feb 22nd, 2008 at 10:09 am.
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Feb 22nd, 2008
0

Re: Make an option stay selected

You're setting RegID out of the Do While Not RS.EOF, so therefore it is never being set, or it is being set to the first record only.

Move it down to within the do while not rs.eof.
SQL = "SELECT * FROM tbregion ORDER BY region ASC"

Set RS = Server.CreateObject("ADODB.recordset")
RS.Open SQL,Conn

Session("region") = Request.Form("regv")
region = Session("region")
%>

<form name="region" action="select_menu.asp" method="post">
<select name="regv" onchange="region.submit()">

<option value="">Make a selection</option>
<option value="">------------------</option>

<%

RegID = RS("regionID")

x=0
Do While Not RS.EOF

If RegID = region Then
For i = 1 To Region
Response.Write("<option value='" & RS("regionID") & "' SELECTED>" & RS("region") & "</option>")
x=x+1
Next

Else

x=x -1
For i = 1 To RegID
Response.Write("<option value='" & RS("regionID") & "'>" & RS("region") & "</option>")
Next
End If


RS.MoveNext
Loop
RS.Close
Set RS = Nothing
%>

</select>
</form>

<%
Response.Write("<a href='category.asp?clevel=1&regv="& region & "'>Link</a>")
%>
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 22nd, 2008
0

Re: Make an option stay selected

Great as usual, THANKS!

Here's a new question within (ok maybe a little outside the scope of the thread).

The sending form is suppuse to send a value back to itself, that means same page. Now, I need to use the exact URL of the page so that value landing at the same spot again. I have tried to put variables in the form action but doesn't help much.

Example:

http://www.webpage.com?category.asp?id=1&anotherid=2

When sending the form value, the only thing happens is that the form value appends to the URL like this:

http://www.webpage.com?category.asp?regv=5

So I'm not at the same place as before.

Any ideas are extremely welcome
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Feb 22nd, 2008
0

Re: Make an option stay selected

I don't understand, please explain a little more.

By changing the mode to POST for the form, nothing will be put in the querystring. You can then check for the form to see if it was submitted by using the request.form method.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 22nd, 2008
1

Re: Make an option stay selected

Also, try adding hidden input fields. It should still send the value through querystring with the GET method.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 22nd, 2008
0

Re: Make an option stay selected

OK, It is my swenglish

Let's say i have clicked on a link and landed at this page with this URL and values
Example: category.asp?id=2&otherid=3
Now, this is a page with specific content set by the appended values:

I wan't to send a value from the dropdown form at that page, and land again at exact page content.

What's happends now is that I land at category.asp?regv=5 (the regv=5 is the form value) and the other appended values is lost

This is the form settings:
<form name="region" action="category.asp" method="get">
<select name="regv" onchange="region.submit()">

I have tried use post also:
<form name="region" action="category.asp" method="post">
<select name="regv" onchange="region.submit()">

After sending the form value I like land at the exact URL again
For example: category.asp?id=2&otherid=3

I have tried to change the form action to append variables but no luck. It only sends the form value
Last edited by TobbeK; Feb 22nd, 2008 at 12:59 pm.
Reputation Points: 10
Solved Threads: 3
Junior Poster
TobbeK is offline Offline
190 posts
since Feb 2008
Feb 22nd, 2008
0

Re: Make an option stay selected

Put the form as POST, otherwise your querystring will be overridden. Then for your form action put :
ASP Syntax (Toggle Plain Text)
  1. action="<%= Request.ServerVariables("SCRIPT_NAME") & "?" & Request.ServerVariables("QUERY_STRING") %>"
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP Forum Timeline: IIS Problem ?
Next Thread in ASP Forum Timeline: Image does not view





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC