473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Viewing the contents of Previous Page using ASP

Vini171285
Hi..

I want to view the contents of previous page,but when i move from second page to first page,it is reloaded and the selected contents are lost..
So what should i do so that when i move from 2nd page to 1st page,all selected contents should remain same..
Actually i tried using session and it is working,but it doesn't work with listboxes.
Is it a good practice to use Session variables.

Thanx
Apr 22 '08 #1
19 1516
DrBunchman
979 Expert 512MB
Hi Vini171285,

Using session variables to retain the values of your controls is a valid and well accepted method. Some people would argue that it would be better to pass the values as hidden fields through a form or the querystring but as long as it works for you I think it's fine.

Can you print the code you're trying to use to set the value of your listbox with a session variable. There's no reason why it shouldn't work so it's probably just a little bug.

Dr B
Apr 22 '08 #2
Hi, DrBunchman
Expand|Select|Wrap|Line Numbers
  1.  
  2. Global.asa
  3. <script language=vbscript runat=server>
  4. Sub Session_onStart()
  5.     Session("name")=""
  6.     Session("pwd")=""
  7.     Session("age")=""
  8. End Sub
  9.  
  10. Sub Session_onEnd
  11.     Session("name")=""
  12.     Session("pwd")=""
  13.     Session("age")=""
  14. End Sub
  15. </script>
  16.  
  17.  
  18. z1.asp
  19. <HTML>
  20. <BODY>
  21. <form method=post action="z2.asp">
  22. Name:<input type=text name=name value=<%=Session("name")%>><BR>
  23. Password:<input type=password name=pwd >
  24. <BR>
  25. Age:<select name=age>
  26.         <option value=1>1 to 10
  27.         <option value=10>11 to 20
  28.         <option value=20>21 to 30
  29.     </select>
  30. <input type=submit>
  31. </form>
  32. </BODY>
  33. </HTML>
  34.  
  35.  
  36.  
  37. z2.asp
  38. <%@ language=VBScript%>
  39. <% option explicit%>
  40. <html>
  41. <body>
  42. <%
  43. Session("name")=Request.Form("name")
  44. Session("pwd")=Request.Form("pwd")
  45. Session("age")=Request.Form("age")
  46. Session.TimeOut=1
  47. Response.Redirect "z3.asp"
  48. %>
  49. </body>
  50. </html>
  51.  
  52.  
  53.  
  54. z3.asp
  55. <html>
  56. <body>
  57. <form method=post action="z1.asp">
  58. Hello<BR>
  59. <BR>
  60. <a href="z1.asp">back</a>
  61. <input type=submit>
  62. </form>
  63. </body>
  64. </html>
  65.  
This is my code.Its just an example..
actual code is very big,in which we are retrieving data from database for listboxes..
and in our project there are 4 pages containing near about 86-87 listboxes..
So ,is Session still appropriate??

Everything is working properly with text box..
but in the case of list box,instead of printing the selected content, listbox is loaded as it is,and the selected value is lost..
What can be done in this situation??

Thanx
Apr 22 '08 #3
DrBunchman
979 Expert 512MB
As I have said the question of whether you should use session variables in this situation is one that divides opinion. There is an article here that outlines some of the pros & cons of using them and I think you'll have to make up your own mind based on your circumstances.

To get your list box to maintain it's state you can use the following example:
Expand|Select|Wrap|Line Numbers
  1.  <select name=age> 
  2. <option value=1 <% If Session("age") = 1 Then Response.Write(" selected ") End If %>>1 to 10</option>
  3. <option value=10 <% If Session("age") = 10 Then Response.Write(" selected ") End If %>>11 to 20</option>
  4. <option value=20<% If Session("age") = 20 Then Response.Write(" selected ") End If %> >21 to 30</option>
  5. </select>
  6.  
Let me know how you get on.

Hope this helps,

Dr B
Apr 22 '08 #4
jeffstl
432 Expert 256MB
Hi..

I want to view the contents of previous page,but when i move from second page to first page,it is reloaded and the selected contents are lost..
So what should i do so that when i move from 2nd page to 1st page,all selected contents should remain same..
Actually i tried using session and it is working,but it doesn't work with listboxes.
Is it a good practice to use Session variables.

Thanx
Session variables was going to be my answer for you. Yes session variables are good practice as long as you clear them when you are done with them.

What do you mean by the session variable doesnt work with the listbox? If you are allowing multiple selections of the list box you need to set up an array variable to handle that probably to hold all the users selections.

Otherwise the only way to pass data is to use the querystring which if you have alot of it can get very tedius. (myaddress.asp?data1=this&data2=this). Or you can use hidden text box fields and get the data with request.form on the next page, etc.
Apr 22 '08 #5
hi..
This is my code when trying to fill list box from database..
Error is in IF stmt(Type mismatch)..
Can u Help..
Code:
Expand|Select|Wrap|Line Numbers
  1.  <%@ language=VBScript%> 
  2. <% option explicit %>
  3. <!--#include virtual="/adovbs.inc"-->
  4. <HTML>
  5. <BODY>
  6. <form method=post action="z2.asp">
  7. Name:<input type=text name=name value=<%=Session("name")%>><BR>
  8. Password:<input type=password name=pwd >
  9. <BR>
  10. Age:<select name=age>
  11. <%
  12.     Dim objConn
  13.     Set objConn=Server.CreateObject("ADODB.Connection")
  14.     objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
  15.     objConn.Open
  16.  
  17.     Dim sql
  18.     sql="SELECT * FROM age"
  19.  
  20.     Dim objRS
  21.     Set objRS=Server.CreateObject("ADODB.Recordset")
  22.     objRS.Open sql,objConn
  23.  
  24.     Do while not objRS.EOF
  25.         Response.Write "<option value='" & objRS("num") & "'"
  26.         If Session("age")=objRS("num") Then
  27.             Response.Write "selected"
  28.         End If
  29.         Response.Write ">"
  30.         Response.Write objRS("age")
  31.         objRS.MoveNext
  32.     Loop
  33.  
  34.     objRS.Close
  35.     Set objRS=Nothing
  36.  
  37.     objConn.Close
  38.     Set objConn=Nothing
  39. %>
  40. </select>
  41.  
  42. <input type=submit>
  43. </form>
  44. </BODY>
  45. </HTML>
  46.  
Apr 23 '08 #6
DrBunchman
979 Expert 512MB
You could try using
Expand|Select|Wrap|Line Numbers
  1. If Session("age") = CInt(objRS("num"))
to cast your record item to a type of integer while comparing it.

Any good?

Dr B
Apr 23 '08 #7
Hi..
Its not working
any other suggestion..
Apr 23 '08 #8
DrBunchman
979 Expert 512MB
Hmm....

What about if you do something like:

Expand|Select|Wrap|Line Numbers
  1.  Dim num 
  2. num = objRS("num")
  3.  
  4. If Session("age") = num Then
  5.  
Dr B
Apr 23 '08 #9
jeffstl
432 Expert 256MB
Hi..
Its not working
any other suggestion..
When you say its not working does that mean you are still getting the type mismatch error or something else? Also which line are you getting that error on?

Since I don't know whats in your database I am going to assume one of the problems may be checking for null?

As far as casting the data type the only time that matters in asp is when your sending the value to a database or a vb class. All variables in asp are automatically variants.

Lastly another thing I noticed was that when you cancatonate (like below) you are using the single quotes. That is for SQL, not for strings. So I removed them for this example. That could be your problem also.

One more thing too and maybe I'm just missing something here...but you dont have the <select> tag before you start writing your <option>?
Oh yeah...a close option tag you were missing too </option> at the end

Expand|Select|Wrap|Line Numbers
  1.     'need <select> tag??
  2.  
  3.     Do while not objRS.EOF
  4.          If Not IsNull(ObjRS("num")) then
  5.              Response.Write "<option value=" & objRS("num") & ""
  6.              If Session("age")=objRS("num") Then
  7.                  Response.Write " selected" 'added a space to seperate
  8.              End If
  9.         else
  10.              Response.Write "<option value=0"
  11.         end if
  12.  
  13.         Response.Write ">" 
  14.         Response.Write objRS("age")
  15.        'then you need a close tag here
  16.        response.write "</option>"
  17.         objRS.MoveNext
  18.     Loop
  19.     'need an </select> tag??
  20.  
  21.  
  22.  
Apr 23 '08 #10
jeffstl
432 Expert 256MB
OK I see your <select> tags now, but you are missing the </option>.
Sorry
Apr 23 '08 #11
Hmm....

What about if you do something like:

Expand|Select|Wrap|Line Numbers
  1.  Dim num 
  2. num = objRS("num")
  3.  
  4. If Session("age") = num Then
  5.  
Dr B

We have tried this also,but its still not working...Its not giving any error,but selected value is lost,list box regains its default value as it is....
Apr 24 '08 #12
This is changed code,but it has the same problem of not retaining values between pages...
It is giving no error....

Expand|Select|Wrap|Line Numbers
  1.  
  2. Code:
  3. <%@ language=VBScript%>
  4. <% option explicit %>
  5. <!--#include virtual="/adovbs.inc"-->
  6. <HTML>
  7. <BODY>
  8. <form method=post action="z2.asp">
  9. Name:<input type=text name=name value=<%=Session("name")%>><BR>
  10. Password:<input type=password name=pwd>
  11. <BR>
  12. Age:<select name=age>
  13. <%
  14.     Dim objConn
  15.     Set objConn=Server.CreateObject("ADODB.Connection")
  16.     objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
  17.     objConn.Open
  18.  
  19.     Dim sql
  20.     sql="SELECT * FROM age"
  21.  
  22.     Dim objRS
  23.     Set objRS=Server.CreateObject("ADODB.Recordset")
  24.     objRS.Open sql,objConn
  25.  
  26.     Do while not objRS.EOF
  27.         If Session("age")=CInt(objRS("num")) Then
  28.             Response.Write "<option value='" & objRS("num") & "' selected>"
  29.         Else
  30.             Response.Write "<option value='" & objRS("num") & "'>"
  31.         End If
  32.         Response.Write objRS("age") & "</option>"
  33.         objRS.MoveNext
  34.     Loop
  35.  
  36.     objRS.Close
  37.     Set objRS=Nothing
  38.  
  39.     objConn.Close
  40.     Set objConn=Nothing
  41. %>
  42. </select>
  43.  
  44. <input type=submit>
  45. </form>
  46. </BODY>
  47. </HTML>
  48.  
Apr 24 '08 #13
jeffstl
432 Expert 256MB
You are still not cancatonating correctly in your strings.

You don't use single quotes for strings, only SQL.

WRONG:
Expand|Select|Wrap|Line Numbers
  1. Do while not objRS.EOF
  2. If Session("age")=CInt(objRS("num")) Then
  3. Response.Write "<option value='" & objRS("num") & "' selected>"
  4. Else
  5. Response.Write "<option value='" & objRS("num") & "'>"
  6. End If
  7. Response.Write objRS("age") & "</option>"
  8. objRS.MoveNext
  9. Loop
  10.  
RIGHT:
Expand|Select|Wrap|Line Numbers
  1. Do while not objRS.EOF
  2. If Session("age")=CInt(objRS("num")) Then
  3. Response.Write "<option value=" & objRS("num") & " selected>"
  4. Else
  5. Response.Write "<option value=" & objRS("num") & ">"
  6. End If
  7. Response.Write objRS("age") & "</option>"
  8. objRS.MoveNext
  9. Loop
  10.  
As far as retaining your values, there is no reason that Session("var") should not work.

Try bringing stuff back to basics to find out why your losing the value. Do some response.write's on your session variables for testing. I would recommend doing a response.write on your Session("age") before your IF statement so you can see whats in there.

If possible just start from scratch for testing purposes just to work with some session variables.

Or do a step through if you have MS Studio and a localhost) and try to determine at what point the value is being lost.

Don't forget if you need to you can also work with hidden text fields and simply get the value with request.form.
Apr 24 '08 #14
DrBunchman
979 Expert 512MB
You are still not cancatonating correctly in your strings.

You don't use single quotes for strings, only SQL.
There's nothing wrong with using single quotes inside a string to delimit your property values.

Expand|Select|Wrap|Line Numbers
  1. Response.Write "<img src='foo.gif' alt='bar' />"
generates perfectly good mark up and as far as I know this would never generate an error.

Unless there's a reason you think otherwise and I'm completely wrong ;-)

Dr B
Apr 24 '08 #15
Hi Jeffstl and DrBunchman,

Jeffstl, as u said presence & absence of Single quotes made no difference...
Dr. Bunchman the solution tat u provided us the last time worked...but as it was a harcoded 1...& v want to retrieve large values from database...this code was not sufficient:::

Code
Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ language=VBScript%>
  3. <% option explicit %>
  4. <!--#include virtual="/adovbs.inc"-->
  5. <HTML>
  6. <BODY>
  7. <form method=post action="z2.asp">
  8. Name:<input type=text name=name value=<%=Session("name")%>><BR>
  9. Password:<input type=password name=pwd>
  10. <BR>
  11. <%
  12. Response.Write CInt(Session("age"))
  13. %><BR>
  14. Age:<select name=age>
  15. <%
  16.     Dim objConn
  17.     Set objConn=Server.CreateObject("ADODB.Connection")
  18.     objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
  19.     objConn.Open
  20.  
  21.     Dim sql
  22.     sql="SELECT * FROM age"
  23.  
  24.     Dim objRS
  25.     Set objRS=Server.CreateObject("ADODB.Recordset")
  26.     objRS.Open sql,objConn
  27.  
  28.  
  29.     Do while not objRS.EOF
  30.         If Session("age")=CInt(objRS("num")) Then
  31.             Response.Write "<option value=" & objRS("num")& " selected>"
  32.             Response.Write objRS("age") & "</option>"
  33.         Else
  34.             Response.Write "<option value=" & objRS("num") & ">"
  35.             Response.Write objRS("age") & "</option>"
  36.         End If
  37.         objRS.MoveNext
  38.     Loop
  39.  
  40.     objRS.Close
  41.     Set objRS=Nothing
  42.  
  43.     objConn.Close
  44.     Set objConn=Nothing
  45. %>
  46. </select>
  47.  
  48. <input type=submit>
  49. </form>
  50. </BODY>
  51. </HTML>
  52.  
I think der is some mistake in writng <option> part...
The session variable actually contains the correct value...but is not displayed...
If v use the back button of the web page..the correct value tat v have selected is displayed...but if v use submit button & go back...the selected value is lost...even though session variable contains correct value...
maybe some problem with button click !!

Thanx !!
Apr 25 '08 #16
DrBunchman
979 Expert 512MB
Hi Vini171285,

Firstly, please remember to use the # button when writing a post to surround your code with code tags as it feels like I've had to edit every sinlge one of your posts in this thread! It makes things much easier to read and helps me out because I don't have to add them in myself!! Thanks :-)

Back to your bug: there's something weird going on here....I tested your code and got the same problem but I can't see where it is. I'm sorry I haven't got more time to look at this right now but hopefully Jeff will be able to or I'll take a look over the weekend.

Regards,

Dr B
Apr 25 '08 #17
DrBunchman
979 Expert 512MB
Just a quick question: your condition is
Expand|Select|Wrap|Line Numbers
  1. If Session("age") = objRS("num")
It shouldn't be
Expand|Select|Wrap|Line Numbers
  1. If Session("age") = objRS("age") 
should it?

Dr B
Apr 25 '08 #18
omerbutt
638 512MB
Just a quick question: your condition is
Expand|Select|Wrap|Line Numbers
  1. If Session("age") = objRS("num")
It shouldn't be
Expand|Select|Wrap|Line Numbers
  1. If Session("age") = objRS("age") 
should it?

Dr B
would it make any difference by not putting the ; sign in your select query
Apr 26 '08 #19
Hi,
Thanx to all...we got the solution for our problem....We just used CInt...and got the solution

Expand|Select|Wrap|Line Numbers
  1. <%@ language=VBScript%>
  2. <% option explicit %>
  3. <!--#include virtual="/adovbs.inc"-->
  4. <HTML>
  5. <BODY>
  6. <form method=post action="z2.asp">
  7. Name:<input type=text name=name value=<%=Session("name")%>><BR>
  8. Password:<input type=password name=pwd>
  9. <BR>
  10. <%
  11. Response.Write CInt(Session("age"))
  12. %><BR>
  13. Age:<select name=age>
  14. <%
  15.     Dim objConn
  16.     Set objConn=Server.CreateObject("ADODB.Connection")
  17.     objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
  18.     objConn.Open
  19.  
  20.     Dim sql
  21.     sql="SELECT * FROM age"
  22.  
  23.     Dim objRS
  24.     Set objRS=Server.CreateObject("ADODB.Recordset")
  25.     objRS.Open sql,objConn
  26.  
  27.     Dim i
  28.     i=CInt(Session("age"))
  29.     Do while not objRS.EOF
  30.         If i=CInt(objRS("num")) Then
  31.             Response.Write "<option value=" & objRS("num")& " selected>"
  32.             Response.Write objRS("age") & "</option>"
  33.         Else
  34.             Response.Write "<option value=" & objRS("num") & ">"
  35.             Response.Write objRS("age") & "</option>"
  36.         End If
  37.         objRS.MoveNext
  38.     Loop
  39.  
  40.     objRS.Close
  41.     Set objRS=Nothing
  42.  
  43.     objConn.Close
  44.     Set objConn=Nothing
  45. %>
  46. </select>
  47.  
  48. <input type=submit>
  49. </form>
  50. </BODY>
  51. </HTML>
Thanx !!
Apr 28 '08 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jeremy Phillips | last post by:
I am trying to build a web form that uses the "POST" method (too much data for GET) to send data to a second form, then displays the response of that form post. The second web form requires...
2
by: Jeremy Phillips | last post by:
I am trying to build a web form that uses the "POST" method (too much data for GET) to send data to a second form, then displays the response of that form post. The second web form requires...
20
by: pembeci | last post by:
I am using JavaScript to modify some text parts of a loaded document. The functions are loaded from a file at the header and run by: <body onLoad="..."> According to the the Venkman profile...
3
by: pagates | last post by:
Hello All, This is a newbie follow-up to my previous newbie question.... Is there a way to view a Word document (or a PDF, or a text file, or any other file) as content for a Master Page? ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.