Viewing the contents of Previous Page using ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vini171285
    New Member
    • Apr 2008
    • 60

    Viewing the contents of Previous Page using ASP

    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
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    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

    Comment

    • Vini171285
      New Member
      • Apr 2008
      • 60

      #3
      Re:

      Hi, DrBunchman
      Code:
       
      Global.asa
      <script language=vbscript runat=server>
      Sub Session_onStart()
      	Session("name")=""
      	Session("pwd")=""
      	Session("age")=""
      End Sub
       
      Sub Session_onEnd
      	Session("name")=""
      	Session("pwd")=""
      	Session("age")=""
      End Sub
      </script>
       
       
      z1.asp
      <HTML>
      <BODY>
      <form method=post action="z2.asp">
      Name:<input type=text name=name value=<%=Session("name")%>><BR>
      Password:<input type=password name=pwd >
      <BR>
      Age:<select name=age>
      		<option value=1>1 to 10
      		<option value=10>11 to 20
      		<option value=20>21 to 30
      	</select>
      <input type=submit>
      </form>
      </BODY>
      </HTML>
       
       
       
      z2.asp
      <%@ language=VBScript%>
      <% option explicit%>
      <html>
      <body>
      <%
      Session("name")=Request.Form("name")
      Session("pwd")=Request.Form("pwd")
      Session("age")=Request.Form("age")
      Session.TimeOut=1
      Response.Redirect "z3.asp"
      %>
      </body>
      </html>
       
       
       
      z3.asp
      <html>
      <body>
      <form method=post action="z1.asp">
      Hello<BR>
      <BR>
      <a href="z1.asp">back</a>
      <input type=submit>
      </form>
      </body>
      </html>
      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
      Last edited by DrBunchman; Apr 22 '08, 10:31 AM. Reason: Added code tags - note the # button

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        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:
        Code:
         <select name=age> 
        <option value=1 <% If Session("age") = 1 Then Response.Write(" selected ") End If %>>1 to 10</option>
        <option value=10 <% If Session("age") = 10 Then Response.Write(" selected ") End If %>>11 to 20</option>
        <option value=20<% If Session("age") = 20 Then Response.Write(" selected ") End If %> >21 to 30</option>
        </select>
        Let me know how you get on.

        Hope this helps,

        Dr B

        Comment

        • jeffstl
          Recognized Expert Contributor
          • Feb 2008
          • 432

          #5
          Originally posted by 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
          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&data 2=this). Or you can use hidden text box fields and get the data with request.form on the next page, etc.

          Comment

          • Vini171285
            New Member
            • Apr 2008
            • 60

            #6
            hi..
            This is my code when trying to fill list box from database..
            Error is in IF stmt(Type mismatch)..
            Can u Help..
            Code:
            Code:
             <%@ language=VBScript%> 
            <% option explicit %>
            <!--#include virtual="/adovbs.inc"-->
            <HTML>
            <BODY>
            <form method=post action="z2.asp">
            Name:<input type=text name=name value=<%=Session("name")%>><BR>
            Password:<input type=password name=pwd >
            <BR>
            Age:<select name=age>
            <%
            	Dim objConn
            	Set objConn=Server.CreateObject("ADODB.Connection")
            	objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
            	objConn.Open
             
            	Dim sql
            	sql="SELECT * FROM age"
             
            	Dim objRS
            	Set objRS=Server.CreateObject("ADODB.Recordset")
            	objRS.Open sql,objConn
             
            	Do while not objRS.EOF
            		Response.Write "<option value='" & objRS("num") & "'"
            		If Session("age")=objRS("num") Then
            			Response.Write "selected"
            		End If
            		Response.Write ">"
            		Response.Write objRS("age")
            		objRS.MoveNext
            	Loop
             
            	objRS.Close
            	Set objRS=Nothing
             
            	objConn.Close
            	Set objConn=Nothing
            %>
            </select>
             
            <input type=submit>
            </form>
            </BODY>
            </HTML>
            Last edited by DrBunchman; Apr 23 '08, 08:46 AM. Reason: Added code tags - note the # button

            Comment

            • DrBunchman
              Recognized Expert Contributor
              • Jan 2008
              • 979

              #7
              You could try using
              Code:
              If Session("age") = CInt(objRS("num"))
              to cast your record item to a type of integer while comparing it.

              Any good?

              Dr B

              Comment

              • Vini171285
                New Member
                • Apr 2008
                • 60

                #8
                Hi..
                Its not working
                any other suggestion..

                Comment

                • DrBunchman
                  Recognized Expert Contributor
                  • Jan 2008
                  • 979

                  #9
                  Hmm....

                  What about if you do something like:

                  Code:
                   Dim num 
                  num = objRS("num")
                   
                  If Session("age") = num Then
                  Dr B

                  Comment

                  • jeffstl
                    Recognized Expert Contributor
                    • Feb 2008
                    • 432

                    #10
                    Originally posted by Vini171285
                    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

                    [code=asp]
                    'need <select> tag??

                    Do while not objRS.EOF
                    If Not IsNull(ObjRS("n um")) then
                    Response.Write "<option value=" & objRS("num") & ""
                    If Session("age")= objRS("num") Then
                    Response.Write " selected" 'added a space to seperate
                    End If
                    else
                    Response.Write "<option value=0"
                    end if

                    Response.Write ">"
                    Response.Write objRS("age")
                    'then you need a close tag here
                    response.write "</option>"
                    objRS.MoveNext
                    Loop
                    'need an </select> tag??


                    [/code]

                    Comment

                    • jeffstl
                      Recognized Expert Contributor
                      • Feb 2008
                      • 432

                      #11
                      OK I see your <select> tags now, but you are missing the </option>.
                      Sorry

                      Comment

                      • Vini171285
                        New Member
                        • Apr 2008
                        • 60

                        #12
                        Originally posted by DrBunchman
                        Hmm....

                        What about if you do something like:

                        Code:
                         Dim num 
                        num = objRS("num")
                         
                        If Session("age") = num Then
                        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....

                        Comment

                        • Vini171285
                          New Member
                          • Apr 2008
                          • 60

                          #13
                          This is changed code,but it has the same problem of not retaining values between pages...
                          It is giving no error....

                          Code:
                           
                          Code:
                          <%@ language=VBScript%>
                          <% option explicit %>
                          <!--#include virtual="/adovbs.inc"-->
                          <HTML>
                          <BODY>
                          <form method=post action="z2.asp">
                          Name:<input type=text name=name value=<%=Session("name")%>><BR>
                          Password:<input type=password name=pwd>
                          <BR>
                          Age:<select name=age>
                          <%
                          	Dim objConn
                          	Set objConn=Server.CreateObject("ADODB.Connection")
                          	objConn.ConnectionString="DSN=Info.dsn;uid=vini;pwd=vini"
                          	objConn.Open
                           
                          	Dim sql
                          	sql="SELECT * FROM age"
                           
                          	Dim objRS
                          	Set objRS=Server.CreateObject("ADODB.Recordset")
                          	objRS.Open sql,objConn
                           
                          	Do while not objRS.EOF
                          		If Session("age")=CInt(objRS("num")) Then
                          			Response.Write "<option value='" & objRS("num") & "' selected>"
                          		Else
                          			Response.Write "<option value='" & objRS("num") & "'>"
                          		End If
                          		Response.Write objRS("age") & "</option>"
                          		objRS.MoveNext
                          	Loop
                           
                          	objRS.Close
                          	Set objRS=Nothing
                           
                          	objConn.Close
                          	Set objConn=Nothing
                          %>
                          </select>
                           
                          <input type=submit>
                          </form>
                          </BODY>
                          </HTML>
                          Last edited by DrBunchman; Apr 24 '08, 05:44 PM. Reason: Added code tags - note the # button

                          Comment

                          • jeffstl
                            Recognized Expert Contributor
                            • Feb 2008
                            • 432

                            #14
                            You are still not cancatonating correctly in your strings.

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

                            WRONG:
                            [code=asp]
                            Do while not objRS.EOF
                            If Session("age")= CInt(objRS("num ")) Then
                            Response.Write "<option value='" & objRS("num") & "' selected>"
                            Else
                            Response.Write "<option value='" & objRS("num") & "'>"
                            End If
                            Response.Write objRS("age") & "</option>"
                            objRS.MoveNext
                            Loop
                            [/code]

                            RIGHT:
                            [code=asp]
                            Do while not objRS.EOF
                            If Session("age")= CInt(objRS("num ")) Then
                            Response.Write "<option value=" & objRS("num") & " selected>"
                            Else
                            Response.Write "<option value=" & objRS("num") & ">"
                            End If
                            Response.Write objRS("age") & "</option>"
                            objRS.MoveNext
                            Loop
                            [/code]

                            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.

                            Comment

                            • DrBunchman
                              Recognized Expert Contributor
                              • Jan 2008
                              • 979

                              #15
                              Originally posted by jeffstl
                              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.

                              Code:
                              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

                              Comment

                              Working...