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
19 1516
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
Hi, DrBunchman -
-
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
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: - <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
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.
hi..
This is my code when trying to fill list box from database..
Error is in IF stmt(Type mismatch)..
Can u Help.. 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>
-
You could try using - If Session("age") = CInt(objRS("num"))
to cast your record item to a type of integer while comparing it.
Any good?
Dr B
Hi..
Its not working
any other suggestion..
Hmm....
What about if you do something like: - Dim num
-
num = objRS("num")
-
-
If Session("age") = num Then
-
Dr B
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 -
'need <select> tag??
-
-
Do while not objRS.EOF
-
If Not IsNull(ObjRS("num")) 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??
-
-
-
OK I see your <select> tags now, but you are missing the </option>.
Sorry
Hmm....
What about if you do something like: - 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....
This is changed code,but it has the same problem of not retaining values between pages...
It is giving no error.... -
-
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>
-
You are still not cancatonating correctly in your strings.
You don't use single quotes for strings, only SQL.
WRONG: -
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
-
RIGHT: -
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
-
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.
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. - 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
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 -
-
<%@ 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>
-
<%
-
Response.Write CInt(Session("age"))
-
%><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>"
-
Response.Write objRS("age") & "</option>"
-
Else
-
Response.Write "<option value=" & objRS("num") & ">"
-
Response.Write objRS("age") & "</option>"
-
End If
-
objRS.MoveNext
-
Loop
-
-
objRS.Close
-
Set objRS=Nothing
-
-
objConn.Close
-
Set objConn=Nothing
-
%>
-
</select>
-
-
<input type=submit>
-
</form>
-
</BODY>
-
</HTML>
-
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 !!
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
Just a quick question: your condition is - If Session("age") = objRS("num")
It shouldn't be - If Session("age") = objRS("age")
should it?
Dr B
Just a quick question: your condition is - If Session("age") = objRS("num")
It shouldn't be - If Session("age") = objRS("age")
should it?
Dr B
would it make any difference by not putting the ; sign in your select query
Hi,
Thanx to all...we got the solution for our problem....We just used CInt...and got the solution - <%@ 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>
-
<%
-
Response.Write CInt(Session("age"))
-
%><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
-
-
Dim i
-
i=CInt(Session("age"))
-
Do while not objRS.EOF
-
If i=CInt(objRS("num")) Then
-
Response.Write "<option value=" & objRS("num")& " selected>"
-
Response.Write objRS("age") & "</option>"
-
Else
-
Response.Write "<option value=" & objRS("num") & ">"
-
Response.Write objRS("age") & "</option>"
-
End If
-
objRS.MoveNext
-
Loop
-
-
objRS.Close
-
Set objRS=Nothing
-
-
objConn.Close
-
Set objConn=Nothing
-
%>
-
</select>
-
-
<input type=submit>
-
</form>
-
</BODY>
-
</HTML>
Thanx !!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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?
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |