form remembers previous text-field values

J

John Devlon

Hi,

I've got a small problem using 2 asp pages.....

The first page has a button to go to the second page.

On the second page, when the page is loaded, a check is excecuted to see if
a session exists. If so, some values will be used to fill some textfields.
When clicking on the button on the second page, the first page appeirs
again.

But when i change te values of the text-field on the second page and click
on the button again to save the new values back to the session, the values
of the previous time are used and not the new one...

Could someone please help me ...?

Thanx

John
 
M

Mark Rae [MVP]

I've got a small problem using 2 asp pages.....

Are these ASP pages or ASP.NET pages...?
On the second page, when the page is loaded, a check is excecuted to see
if a session exists.

A session will always exist - do you mean that a check is executed to see if
a particular sssion variable exists...?
But when i change te values of the text-field on the second page and click
on the button again to save the new values back to the session, the values
of the previous time are used and not the new one...

On the assumption that you're using ASP.NET and not ASP, are you fetching
the values from session every time the second page loads, even if as a
result of a postback...?

Also, AAMOI, is there any particular reason that you're using two pages for
this...?
 
J

John Devlon

Dear Mark,

Yes, I'm using asp.net... And I'm checking for a partical session
variable...

Every time, the second page is loaded, some information is read from the
session variable...

I just would like to know why the second time the page is visited and a
button is clicked, the values of the first time is used, and not the new
ones ...
How can i prevent this ?

John
 
M

Mark Rae [MVP]

Yes, I'm using ASP.NET... And I'm checking for a particalar session
variable...

OK. For future reference, it really helps if you use the correct terms for
things, otherwise people constantly have to ask you what you really mean...
Every time, the second page is loaded, some information is read from the
session variable...

I just would like to know why the second time the page is visited and a
button is clicked, the values of the first time is used, and not the new
ones ...
How can I prevent this ?

It's a little difficult to say for sure as you haven't shown any of your
code, but I'm suspecting that you've forgotten to wrap your Page_Load code
in an if (!IsPostBack) loop...
 
J

John Devlon

Dear Mark,

Code on the first page:

Protected Sub btnCheckOut_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnCheckOut.Click
Response.Redirect("CheckOut.aspx")
End Sub


Second page:
Partial Class CheckOut
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myCustomer As New Customer
If Session("Customer") IsNot Nothing Then
Me.txtFirstName.Text =
Session("Customer").Firstname()
Me.txtLastName.Text =
Session("Customer").LastName()
Me.txtMail.Text =
Session("Customer").Email()
End If
End Sub

Protected Sub btnShopping_Click(ByVal sender As Object, ByVal e
As System.EventArgs) Handles btnShopping.Click
If Session("Customer") Is Nothing And Page.IsValid Then
Dim myNewCustomer As New Customer
myNewCustomer.FirstName = Me.txtFirstName.Text
myNewCustomer.LastName = Me.txtLastName.Text
myNewCustomer.Email = Me.txtMail.Text
Session.Add("Customer", myNewCustomer)
Else
Session("Customer").Firstname() =
Me.txtFirstName.Text
Session("Customer").LastName() =
Me.txtLastName.Text
Session("Customer").Email() = Me.txtMail.Text
End If

Response.Redirect("cart.aspx")
End Sub
End Class
 
M

Mark Rae [MVP]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myCustomer As New Customer
If Session("Customer") IsNot Nothing Then
Me.txtFirstName.Text =
Session("Customer").Firstname()
Me.txtLastName.Text =
Session("Customer").LastName()
Me.txtMail.Text =
Session("Customer").Email()
End If
End Sub

As suspected, the problem is that you're running the code in Page_Load every
time the page loads, even on postback, so you're overwriting all your
changes...

If Not IsPostBack Then
Dim myCustomer As New Customer
If Session("Customer") IsNot Nothing Then
Me.txtFirstName.Text = Session("Customer").Firstname()
Me.txtLastName.Text = Session("Customer").LastName()
Me.txtMail.Text = Session("Customer").Email()
End If
End If
 
J

John Devlon

Thanx Mark,

It works great

John




Mark Rae said:
As suspected, the problem is that you're running the code in Page_Load
every time the page loads, even on postback, so you're overwriting all
your changes...

If Not IsPostBack Then
Dim myCustomer As New Customer
If Session("Customer") IsNot Nothing Then
Me.txtFirstName.Text = Session("Customer").Firstname()
Me.txtLastName.Text = Session("Customer").LastName()
Me.txtMail.Text = Session("Customer").Email()
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top