Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 January 2nd, 2005, 12:04 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default Server.Transfer not maintaining ViewState

First, I apologize for the cross-post. A kind user pointed out that I posted originally in the wrong forum (ASP Web How-to). This is an ASP.Net question.

I'm doing my own form validation, and if the validation fails, I use a Server.Transfer method to send control back to the originating form.

Once there, the load event handler will see that an error flag has been set and tell the user what they did wrong.

Only problem is all the form's textbox controls go blank after the form reloads, even though their EnableViewState properties are set to True. My question is this:

Is Server.Transfer the wrong method to return control back to the originating form? If so, what is the preferred method? I am using the True parameter, i.e.

Server.Transfer("OriginalURL.aspx", True)

Do I need to set each control's Text property on page load, kind of like I used to do in classic ASP?

Thanks for your help in advance!

HAPPY NEW YEAR!!!

Aaron


 
Old January 3rd, 2005, 12:38 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi guys,

I made my question more complicated that it needs to be. It's just this:

How do you return control back to the browser?

Server.Transfer, True
Response.Redirect
Return

Which of these methods will preserve ViewState?

Thanks.

 
Old January 3rd, 2005, 03:01 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

This may not be an answer to your question directly, but why do you need to transfer to another page in the first place?

Usually, you just post back to the same page and perform validation. If it succeeds, you redirect to another page. If it fails, you let the page load again, and you'll find all controls have maintained their state. IMO, that's how you should do it in the .NET world.

AFAICS, Server.Transfer and Response.Redirect both won't cut it. Response.Redirect issues a client side transfer, so you'll loose all page state anyway.

Server.Transfer, even with the preserveForm option set to True also looses control state. What it does preserve is the form values which is different from View state / Control state. Basically, when you use Server.Transfer you can use Request.Form("YourControl") in the page you're transfering to, as opposed to the strongly typed version of YourControl.Text, for example.

ViewState will only be available when you post back to the same page.

Not sure what you mean with Return.

HtH and a happy new year to you too.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: North West Three by Fatboy Slim (Track 8 from the album: Palookaville) What's This?
 
Old January 3rd, 2005, 04:10 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Yes, you got me on the right track. If I post back to the same page the control states are indeed all maintained. the only problem is that the Form's Load event never gets triggered and so my page's initialization never occurs.

Basically the initialization checks a public shared ErrorFlag varaiable that I set up as a Boolean, and if it's True, it displays a message telling them that there was an error (usually a password was wrong or something like that).

So...is there either:

1) another event that I can put my initialization in, some event that triggers even if Load doesn't trigger?

2) A way to reload the page that forces the Load event to trigger, i.e. a Refresh?

Thanks

 
Old January 3rd, 2005, 04:31 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Heuh? Page_Load not firing? Are you sure? This is not the normal situation, so can you post the code for the page so we can take a look at it? Maybe the handler for the load event got deleted (VS.NET has a habit of doing that).

What do you need a public static variable for? IMO, all you need is a private, instance scoped bool to track the errors. Again, if you post some code, we might come up with a better solution.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Sleeping With Ghosts by Placebo (Track 4 from the album: Sleeping with Ghosts) What's This?
 
Old January 3rd, 2005, 06:50 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay. I'll post the ASPX and the Codebehind in two blocks. I'm just posting the first page (it's an 8 page form). It's basically the same structure for each page.

THANK YOU!!!!

Aaron

--------------------------Here's the ASPX----------------------------

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="JobApp1.aspx.vb" Inherits="JobApplication.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>Glide Employment Application</title>
        <meta content="False" name="vs_showGrid">
        <meta content="True" name="vs_snapToGrid">
        <style type="text/css">BODY { FONT-SIZE: 10pt; FONT-FAMILY: arial, helvetica, sans-serif }
        </style>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:label id="Label1" style="Z-INDEX: 101; LEFT: 184px; POSITION: absolute; TOP: 16px" runat="server"
                Font-Size="14pt" Font-Names="Arial,Helvetica,sans-serif" Font-Bold="True" Height="40px"
                Width="416px">Glide Foundation Employment Application</asp:label><asp:button id="btnNext" style="Z-INDEX: 133; LEFT: 259px; POSITION: absolute; TOP: 480px" tabIndex="12"
                runat="server" Text="Next"></asp:button><asp:button id="btnSave" style="Z-INDEX: 135; LEFT: 320px; POSITION: absolute; TOP: 480px" tabIndex="12"
                runat="server" Text="Save for Later"></asp:button><asp:label id="lblZip" style="Z-INDEX: 132; LEFT: 528px; POSITION: absolute; TOP: 440px" runat="server"
                Font-Size="8pt" Width="26px">Zip</asp:label><asp:label id="lblState" style="Z-INDEX: 131; LEFT: 474px; POSITION: absolute; TOP: 440px"
                runat="server" Font-Size="8pt" Width="26px">ST</asp:label><asp:label id="lblCity" style="Z-INDEX: 130; LEFT: 304px; POSITION: absolute; TOP: 440px" runat="server"
                Font-Size="8pt" Width="104px">City</asp:label><asp:label id="lblStreet" style="Z-INDEX: 129; LEFT: 136px; POSITION: absolute; TOP: 440px"
                runat="server" Font-Size="8pt" Width="104px">Street & Apt.</asp:label><asp:textbox id="tbZip" style="Z-INDEX: 128; LEFT: 528px; POSITION: absolute; TOP: 416px" tabIndex="11"
                runat="server" Width="66px" MaxLength="10"></asp:textbox><asp:textbox id="tbState" style="Z-INDEX: 127; LEFT: 472px; POSITION: absolute; TOP: 416px" tabIndex="10"
                runat="server" Width="26px" MaxLength="2"></asp:textbox><asp:textbox id="tbCity" style="Z-INDEX: 126; LEFT: 304px; POSITION: absolute; TOP: 416px" tabIndex="9"
                runat="server" Width="152px" MaxLength="50"></asp:textbox><asp:textbox id="tbStreet" style="Z-INDEX: 125; LEFT: 138px; POSITION: absolute; TOP: 416px"
                tabIndex="8" runat="server" Width="152px" MaxLength="100"></asp:textbox><asp:label id="lblAddress" style="Z-INDEX: 124; LEFT: 32px; POSITION: absolute; TOP: 424px"
                runat="server" Height="16px" Width="56px">Address:</asp:label><asp:textbox id="tbEmail" style="Z-INDEX: 123; LEFT: 216px; POSITION: absolute; TOP: 360px" tabIndex="7"
                runat="server" Width="90px" MaxLength="50"></asp:textbox><asp:label id="lblEmail" style="Z-INDEX: 122; LEFT: 32px; POSITION: absolute; TOP: 360px" runat="server"
                Height="16px" Width="144px">Email Address:</asp:label><asp:label id="lblEveningPhone" style="Z-INDEX: 121; LEFT: 32px; POSITION: absolute; TOP: 328px"
                runat="server" Height="16px" Width="168px">Evening Phone Number:</asp:label><asp:textbox id="tbEveningPhone" style="Z-INDEX: 120; LEFT: 216px; POSITION: absolute; TOP: 328px"
                tabIndex="6" runat="server" Width="90px" MaxLength="50"></asp:textbox><asp:label id="Label8" style="Z-INDEX: 117; LEFT: 32px; POSITION: absolute; TOP: 296px" runat="server"
                Height="16px" Width="144px">Daytime Phone Number:</asp:label><asp:textbox id="tbDaytimePhone" style="Z-INDEX: 119; LEFT: 216px; POSITION: absolute; TOP: 296px"
                tabIndex="5" runat="server" Width="90px" MaxLength="50"></asp:textbox><asp:label id="lblDaytimePhone" style="Z-INDEX: 118; LEFT: 32px; POSITION: absolute; TOP: 296px"
                runat="server" Height="16px" Width="168px">Daytime Phone Number:</asp:label><asp:label id="lblSocial" style="Z-INDEX: 115; LEFT: 32px; POSITION: absolute; TOP: 264px"
                runat="server" Height="16px" Width="168px">Social Security Number:</asp:label><asp:label id="lblMiddle" style="Z-INDEX: 113; LEFT: 440px; POSITION: absolute; TOP: 232px"
                runat="server" Font-Size="8pt" Width="104px">Middle</asp:label><asp:label id="lblFirst" style="Z-INDEX: 112; LEFT: 264px; POSITION: absolute; TOP: 232px"
                runat="server" Font-Size="8pt" Width="104px">First</asp:label><asp:textbox id="tbMiddleName" style="Z-INDEX: 110; LEFT: 440px; POSITION: absolute; TOP: 208px"
                tabIndex="3" runat="server" Width="112px" MaxLength="50"></asp:textbox><asp:textbox id="tbFirstName" style="Z-INDEX: 109; LEFT: 264px; POSITION: absolute; TOP: 208px"
                tabIndex="2" runat="server" MaxLength="50"></asp:textbox><asp:textbox id="tbLastName" style="Z-INDEX: 108; LEFT: 80px; POSITION: absolute; TOP: 208px"
                tabIndex="1" runat="server" MaxLength="50"></asp:textbox>

            <DIV style="DISPLAY: inline; FONT-WEIGHT: bold; Z-INDEX: 103; LEFT: 24px; WIDTH: 184px; FONT-FAMILY: ariel, helvetica, sans-serif; POSITION: absolute; TOP: 160px; HEIGHT: 24px"
                ms_positioning="FlowLayout">
                <P>Contact Information:</P>
            </DIV>
            <asp:image id="Image1" style="Z-INDEX: 102; LEFT: 632px; POSITION: absolute; TOP: 16px" runat="server"
                Height="96px" Width="136px" ImageUrl="file:///C:\Inetpub\wwwroot\JobApplication\logo_icon.jpg"></asp:image>
            <DIV style="DISPLAY: inline; Z-INDEX: 105; LEFT: 600px; WIDTH: 112px; POSITION: absolute; TOP: 160px; HEIGHT: 32px"
                ms_positioning="FlowLayout">
                <P>Application Date:</P>
            </DIV>
            <asp:label id="lblDate" style="Z-INDEX: 106; LEFT: 712px; POSITION: absolute; TOP: 160px" runat="server"
                Height="24px" Width="130px">Label</asp:label><asp:label id="lblName" style="Z-INDEX: 107; LEFT: 32px; POSITION: absolute; TOP: 208px" runat="server"
                Height="16px" Width="40px">Name:</asp:label><asp:label id="lblLast" style="Z-INDEX: 111; LEFT: 80px; POSITION: absolute; TOP: 232px" runat="server"
                Font-Size="8pt" Width="104px">Last</asp:label>
            <DIV style="DISPLAY: inline; FONT-WEIGHT: bold; Z-INDEX: 114; LEFT: 328px; WIDTH: 112px; FONT-FAMILY: ariel, helvetica, sans-serif; POSITION: absolute; TOP: 128px; HEIGHT: 24px"
                ms_positioning="FlowLayout">
                <P style="FONT-SIZE: 14pt">Page 1 of 8</P>
            </DIV>
            <asp:textbox id="tbSocial" style="Z-INDEX: 116; LEFT: 216px; POSITION: absolute; TOP: 264px"
                tabIndex="4" runat="server" Width="90px" MaxLength="11"></asp:textbox><asp:label id="lblPageIntro" style="Z-INDEX: 134; LEFT: 24px; POSITION: absolute; TOP: 56px"
                runat="server" Height="56px" Width="408px">Please fill out as much information as possible. Leave items blank that do not apply. You may save this application for completion at a later date. This form is secured and confidential.</asp:label><asp:hyperlink id="HyperLink1" style="Z-INDEX: 136; LEFT: 576px; POSITION: absolute; TOP: 215px"
                runat="server" Width="136px" NavigateUrl="LoadApplication.aspx">Load Saved Application</asp:hyperlink>
            <asp:Label id="lblInvalidEmail" style="Z-INDEX: 137; LEFT: 320px; POSITION: absolute; TOP: 360px"
                runat="server" Font-Bold="True" Height="24px" Width="224px" ForeColor="Red" Visible="False">Does not appear to be valid</asp:Label>
            <asp:Label id="lblQuery" style="Z-INDEX: 138; LEFT: 352px; POSITION: absolute; TOP: 264px"
                runat="server" Width="256px" Height="80px">Label</asp:Label></form>
    </body>
</HTML>

-----------------------And Here's the Codebehind--------------------

Imports System.Data
Imports System.Data.OleDb
Public Class WebForm1
    Inherits System.Web.UI.Page
    Public Structure PageErrors
        Public LastName As Boolean
        Public FirstName As Boolean
        Public Social As Boolean
        Public DayPhone As Boolean
        Public EvePhone As Boolean
        Public Email As Boolean
        Public ErrorFlag As Boolean
        Sub InitializeErrors()
            LastName = False
            FirstName = False
            Social = False
            DayPhone = False
            EvePhone = False
            Email = False
            ErrorFlag = False
        End Sub
    End Structure
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents lblDate As System.Web.UI.WebControls.Label
    Protected WithEvents tbLastName As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbFirstName As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbMiddleName As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbSocial As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbDaytimePhone As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label8 As System.Web.UI.WebControls.Label
    Protected WithEvents tbEveningPhone As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbEmail As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnNext As System.Web.UI.WebControls.Button
    Protected WithEvents tbZip As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbState As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbCity As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbStreet As System.Web.UI.WebControls.TextBox
    Protected WithEvents DIV1 As System.Web.UI.HtmlControls.HtmlGenericControl
    Protected WithEvents lblZip As System.Web.UI.WebControls.Label
    Protected WithEvents lblState As System.Web.UI.WebControls.Label
    Protected WithEvents lblCity As System.Web.UI.WebControls.Label
    Protected WithEvents lblStreet As System.Web.UI.WebControls.Label
    Protected WithEvents lblAddress As System.Web.UI.WebControls.Label
    Protected WithEvents lblEmail As System.Web.UI.WebControls.Label
    Protected WithEvents lblEveningPhone As System.Web.UI.WebControls.Label
    Protected WithEvents lblDaytimePhone As System.Web.UI.WebControls.Label
    Protected WithEvents lblSocial As System.Web.UI.WebControls.Label
    Protected WithEvents lblMiddle As System.Web.UI.WebControls.Label
    Protected WithEvents lblFirst As System.Web.UI.WebControls.Label
    Protected WithEvents lblName As System.Web.UI.WebControls.Label
    Protected WithEvents lblLast As System.Web.UI.WebControls.Label
    Protected WithEvents lblPageIntro As System.Web.UI.WebControls.Label
    Public Shared Page1Errors As PageErrors
    Public Shared Loaded As Boolean
    Public Shared ApplicantID As Int32
    Protected WithEvents btnSave As System.Web.UI.WebControls.Button
    Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
    Protected WithEvents lblInvalidEmail As System.Web.UI.WebControls.Label
    Protected WithEvents lblQuery As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lblDate.Text = Today.ToShortDateString.ToString

        If Page1Errors.ErrorFlag = True Then
            lblPageIntro.Text = "There was a problem with your submission, please correct or enter the fields marked in red."
            lblPageIntro.ForeColor = System.Drawing.Color.Red
            lblPageIntro.Font.Bold = True

            If Page1Errors.LastName = True Then
                lblLast.ForeColor = System.Drawing.Color.Red
                lblLast.Font.Bold = True
            End If
            If Page1Errors.FirstName = True Then
                lblFirst.ForeColor = System.Drawing.Color.Red
                lblFirst.Font.Bold = True
            End If
            If Page1Errors.Social = True Then
                lblSocial.ForeColor = System.Drawing.Color.Red
                lblSocial.Font.Bold = True
            End If
            If Page1Errors.Email = True Then
                lblEmail.ForeColor = System.Drawing.Color.Red
                lblEmail.Font.Bold = True
                lblInvalidEmail.Visible = True
            End If

        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        'ValidatePage1()
        'If Page1Errors.ErrorFlag = True Then Response.Redirect("JobApp1.aspx", True)
        SaveToDatabase()
        Response.Redirect("JobApp2.aspx", False)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ValidatePage1()
        SaveToDatabase()
        'go to page that prompts for password
    End Sub

    Sub ValidatePage1()
        Page1Errors.InitializeErrors()
        If tbLastName.Text = "" Then
            Page1Errors.LastName = True
            Page1Errors.ErrorFlag = True
        End If
        If tbFirstName.Text = "" Then
            Page1Errors.FirstName = True
            Page1Errors.ErrorFlag = True
        End If
        'Validate Social
        Dim strSocial As String = tbSocial.Text
        strSocial = Replace(strSocial, "-", "")
        strSocial = Replace(strSocial, ".", "")
        If Len(strSocial) <> 9 Or Not IsNumeric(strSocial) Then
            Page1Errors.Social = True
            Page1Errors.ErrorFlag = True
        End If
        If Not tbEmail.Text = "" Then
            If Not IsValidEmail(tbEmail.Text) Then
                Page1Errors.Email = True
                Page1Errors.ErrorFlag = True
            End If
        End If
    End Sub

    Function IsValidEmail(ByVal email) As Boolean
        Dim EmailRegex As New System.Text.RegularExpressions.Regex("^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$")
        Dim M As System.Text.RegularExpressions.Match = EmailRegex.Match(email)
        If M.Success Then
            Return True
        Else
            Return False
        End If
    End Function

    Sub SaveToDatabase()
        Dim cnxn As New OleDbConnection
        cnxn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
         "Data Source=" & Server.MapPath("Applicants.mdb")
        cnxn.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = cnxn
        With cmd.Parameters
            .Add(New OleDbParameter("@FirstName", tbFirstName.Text))
            .Add(New OleDbParameter("@LastName", tbLastName.Text))
            .Add(New OleDbParameter("@MiddleName", tbMiddleName.Text))
            .Add(New OleDbParameter("@Social", tbSocial.Text))
            .Add(New OleDbParameter("@DayPhone", tbDaytimePhone.Text))
            .Add(New OleDbParameter("@EvePhone", tbEveningPhone.Text))
            .Add(New OleDbParameter("@Email", tbEmail.Text))
            .Add(New OleDbParameter("@Street", tbStreet.Text))
            .Add(New OleDbParameter("@City", tbCity.Text))
            .Add(New OleDbParameter("@State", tbState.Text))
            .Add(New OleDbParameter("@Zip", tbZip.Text))
        End With
        If Loaded = False Then
            cmd.CommandText = "INSERT INTO tblApplicants (txtFirstName, txtLastName, txtMiddleName, txtSocial, txtDaytimePhone, txtEveningPhone, txtEmail, txtStreetAddress, txtCity, txtState, txtZip)" & _
                " VALUES (@FirstName, @LastName, @MiddleName, @Social, @DayPhone, @EvePhone, @Email, @Street, @City, @State, @Zip)"
        Else
            cmd.CommandText = "UPDATE tblApplicants SET txtFirstName=@FirstName, txtLastName=@LastName, txtMiddleName=@MiddleName, txtSocial=@Social, txtDaytimePhone=@DayPhone, " & _
                "txtEveningPhone=@EvePhone, txtEmail=@Email, txtStreetAddress=@Street, txtCity=@City, txtState=@State, txtZip=@Zip WHERE pkApplicantID=" & ApplicantID
        End If
        cmd.ExecuteNonQuery()
        'Get the pkApplicantID of the record we just inserted so we can use it for updates on all subsequent pages
        cmd.CommandText = "SELECT pkApplicantID From tblApplicants ORDER BY pkApplicantID DESC"
        ApplicantID = cmd.ExecuteScalar
        cmd.Dispose()
        cnxn.Dispose()
    End Sub

End Class


 
Old January 3rd, 2005, 07:46 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Woops! I accidentally posted the "Current Workaround" version, instead of the version I have been trying to get working. It's the exact same except the btnNext_Click event handler is subtly different as follows:

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        ValidatePage1()
        If Page1Errors.ErrorFlag = False Then
            SaveToDatabase()
            Server.Transfer("JobApp2.aspx", False)
        End If
    End Sub


 
Old January 4th, 2005, 12:08 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

***LATEST NEWS***

After some experimentation, I've deduced that the Load event is in fact firing just like it should. The problem is with sharing data between pages using public shared members. As you can see from the code below, I set up a structure called PageErrors:

    Public Structure PageErrors
        Public LastName As Boolean
        Public FirstName As Boolean
        Public Social As Boolean
        Public DayPhone As Boolean
        Public EvePhone As Boolean
        Public Email As Boolean
        Public ErrorFlag As Boolean
        Sub InitializeErrors()
            LastName = False
            FirstName = False
            Social = False
            DayPhone = False
            EvePhone = False
            Email = False
            ErrorFlag = False
        End Sub
    End Structure

Then I create a public shared instance of it called Page1Errors as follows:

     Public Shared Page1Errors As PageErrors

Then during validation I first run the initialize method, setting al errors to false, and run my tests, setting the appropriate errors to true.

All good, but the page then reloads, the Load event fires as follows:

        lblDate.Text = Today.ToShortDateString.ToString
        If Page1Errors.ErrorFlag = True Then
            lblPageIntro.Text = "There was a problem with your submission, please correct or enter the fields marked in red."
            lblPageIntro.ForeColor = System.Drawing.Color.Red
            lblPageIntro.Font.Bold = True

            If Page1Errors.LastName = True Then
                lblLast.ForeColor = System.Drawing.Color.Red
                lblLast.Font.Bold = True
            End If
            ...
          End If

...And so forth until I've gone through the whole collection.

OKay, so the weirdness is that after the page reloads, it doesn't see Page1Errors.ErrorFlag as being set to True, so it skips the rest of it. The funny thing is though, that if I then refresh my browser by hitting F5, it then sees the Page1Errors object and correctly displays my error messages.

I also tried putting the Page1Errors object into a session variable, and passing it that way but that also didn't work.

Since I've experienced a similar situation before when using public shared members to pass data between pages, it looks like I lack a fundamental understanding of state management and maybe the whole private, public, shared concepts.

Any help or guidance in this area would be appreciated.

Thanks.

Aaron







 
Old January 4th, 2005, 05:24 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I think you're abusing Shared Members. You should see shared members as properties of classes, and not of instances of those classes. So, Shared Members apply to the entire app. It may work on your local dev machine because you're the only one using the app, but you'll get in trouble when you go live as multiple users share the same variable (hence the name Shared Members ;)).

If you have a one page wizard, you can store temporary variables in ViewState, like this:

ViewState("MyVar") = MyValue

and later retrieve it again (after postback)

MyValue = CType(ViewState("MyVar"), YourType)

If you're using a multi-page wizard, you need to pass "state" in another way. One way is to use session variables, another is to use the QueryString, like this:

Response.Redirect("Page2.aspx?STATUS=OK")

Even better: don't redirect to the next page when you haven't completed the first page:

Page 1
' Validate here
If IsValid Then
  Response.Redirect("Page2.aspx")
Else
  ' Display error messages
End If

Does this help??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Crazy Life by Hed Planet Earth (Track 6 from the album: Blackout ) What's This?
 
Old January 4th, 2005, 08:49 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Totally helps!

I just figured out that I can display all the error messages BEFORE returning contol to the client, as in

If ErrorFlag=False Then
  Server.Transfer("NextPage.aspx")
Else
  lblError.Text = "You jerk! Try putting in some REAL Data!"
End If

This is so counter-classic ASP! It's also much cooler. In classic ASP you HAD to make your changes on page-load or they would never display. This way is much better.

My only question is about what you said about shared members with multiple simultaneous users. Isn't a shared member unique to each user, with each user's session pointing to a unique instance of the Application's class?

I like your suggestion for passing values using ViewState, although I don't know what a one page or multi-page wizard is (is that part of Visual Studio?).








Similar Threads
Thread Thread Starter Forum Replies Last Post
Server Side Viewstate in ASP.NET 2.0 vikaspatyal ASP.NET 2.0 Professional 1 December 18th, 2007 12:26 AM
Maintaining the viewstate in a custom web control cridley C# 0 February 20th, 2006 07:55 AM
xmlHttp maintaining session with server opacey Excel VBA 0 September 8th, 2005 10:37 AM
Transfer Data from one server to another server Jane SQL Server 2000 2 February 28th, 2005 03:15 AM
ViewState with Server.Transfer Aaron Edwards Classic ASP Basics 3 January 2nd, 2005 07:52 PM





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