Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2006, 02:56 PM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Variables losing their values

I am trying to create an aspx page that shows multiple forms to the user. To do that, I have a pagenumber variable that I increment. Then I use If Then lines to display the appropriate user input pages.

The problem that I am running into is that each time a user clicks on the Next button to show the next page, I lose values from variables that stored previous page inputs. Below is an example of my coding scheme. By the time it loads the PageNumber=3 info, the UserName variable has lost its value. How do I keep the values applied to the variables?

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<Script runat="server">

Public PageNumber as Integer=1
Public UserName, Phone as String

Sub Page1Next(Sender As Object, e As EventArgs)
    UserName=txtName.Text
    PageNumber=2
End Sub

Sub Page2Next(Sender As Object, e As EventArgs)
    Phone=txtPhone.Text
    PageNumber=3
End Sub
</Script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Test</title>
</head>
<body>

<form method="POST" runat="server">

<% if PageNumber=1 then %>
Enter Name <asp:TextBox ID="txtName" Runat="server" />
<asp:Button id="btnNext1" Text="Next" Runat="server" OnClick="Page1Next" />
<% end if %>

<% if PageNumber=2 then %>
Enter Phone Number <asp:TextBox ID="txtPhone" Runat="server" />
<asp:Button id="btnNext2" Text="Next" Runat="server" OnClick="Page2Next" />
<% end if %>

<% if PageNumber=3 then %>
Enter City <asp:TextBox ID="txtCity" Runat="server" />
<% end if %>

</form>
</body>
</html>

 
Old April 12th, 2006, 10:44 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

to persist values from page to page you need to use session variables, cookies or a query string. Also, some of the code you have is classic asp coding

 
Old April 13th, 2006, 08:44 AM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will look into setting up session variables. Thanks for the suggestion. As you can probably tell, I'm picking up ASP.NET as I go along, so I'm sure I'm not coding in a standardized way.

 
Old April 13th, 2006, 01:09 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I know it's hard loosing old habits. Remember, any logic should be put in the code behind or a class etc. Embedded logic is the way of the past, and will just cause problems. There are several advantages to having a code behind file.






Similar Threads
Thread Thread Starter Forum Replies Last Post
losing the leading zero in binary values giles BOOK: Beginning Java 2, JDK 5 Edition 2 September 22nd, 2005 11:41 AM
Losing variables bluemat XSLT 2 October 16th, 2004 01:59 AM
Losing form variables (from posting) BunaZiua Dreamweaver (all versions) 1 February 23rd, 2004 04:46 PM
Losing session variables Justine Classic ASP Professional 3 January 16th, 2004 10:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.