jeudi 24 septembre 2015

Unexpected behavior in ASP.NET VS2013 on handling Server.Transfer and Setting IsPostBack

In VS2013 using Framework 4.5, I experienced what I consider to be unexpected behavior using server.transfer's from button clicks as I traversed from page to page. To illustrate here, I started a new VS2013 Web Site in Visual Basic using the ASP.NET Web Forms Site Template.

I added three new web Forms as Test1.aspx, Test2.aspx and Test3.aspx, each using the auto-generated Site.Master. The page code for each is:

Test1.aspx:

hfTest0 (Coming  In): <asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
<br />
hfTest1 (Coming In): <asp:Label ID="lbl3" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="btnTest" runat="server" Text="GoTo Test2" />
<br />
hfTest0 (After Load): <asp:Label ID="lbl2" runat="server" Text=""></asp:Label>
<br />
hfTest2 (After Load): <asp:Label ID="lbl4" runat="server" Text=""></asp:Label>

Test2.aspx:

hfTest0 (Coming  In): <asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
<br />
hfTest1 (Coming In): <asp:Label ID="lbl3" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="btnTest" runat="server" Text="GoTo Test3" />
<br />
hfTest0 (After Load): <asp:Label ID="lbl2" runat="server" Text=""></asp:Label>
<br />
hfTest2 (After Load): <asp:Label ID="lbl4" runat="server" Text=""></asp:Label>

Test3.aspx:

hfTest0 (Coming  In): <asp:Label ID="lbl1" runat="server" Text=""></asp:Label>
<br />
hfTest1 (Coming In): <asp:Label ID="lbl3" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="btnTest" runat="server" Text="GoTo Test1" />
<br />
hfTest0 (After Load): <asp:Label ID="lbl2" runat="server" Text=""></asp:Label>
<br />
hfTest2 (After Load): <asp:Label ID="lbl4" runat="server" Text=""></asp:Label>

To support the above references to the hidden fields of hfTest0 and hfTest2, I added these fields to the Site.Master Page as:

Site.Master:

<asp:HiddenField ID="hfTest0" runat="server" Value=""/> 
<asp:HiddenField ID="hfTest1" runat="server" Value=""/>

The above two fields are examples of what I am trying to achieve in terms of maintaing state when transferring from page to page.

For the Code-Behind in the three test pages I included the following blocks of code in the Sub for the Load Event as:

Test1.aspx.vb

        If IsPostBack Then

            Exit Sub

        End If

        If Me.PreviousPage Is Nothing Then

            lbl1.Text = ""

            lbl3.Text = ""

            CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

            CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "one"
        Else

            lbl1.Text = CType(Me.PreviousPage.Master.FindControl("hfTest0"), HiddenField).Value

            lbl3.Text = CType(Me.PreviousPage.Master.FindControl("hfTest1"), HiddenField).Value

            If CType(Me.PreviousPage.Master.FindControl("hfTest0"), HiddenField).Value = "Active" Then

                CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

                CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "ONE"
            Else
                CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

                CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "one"
            End If
        End If

    lbl2.Text = CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value

    lbl4.Text = CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value

Test2.aspx.vb

        If IsPostBack Then

            Exit Sub

        End If

        If Me.PreviousPage Is Nothing Then

            lbl1.Text = ""

            lbl3.Text = ""

            CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

            CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "two"
        Else

            lbl1.Text = CType(Me.PreviousPage.Master.FindControl("hfTest0"), HiddenField).Value

            lbl3.Text = CType(Me.PreviousPage.Master.FindControl("hfTest1"), HiddenField).Value

            If CType(Me.PreviousPage.Master.FindControl("hfTest0"), HiddenField).Value = "Active" Then

                CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

                CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "TWO"
            Else
                CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

                CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "two"
            End If
        End If

    lbl2.Text = CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value

    lbl4.Text = CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value

Test3.aspx.vb

        If IsPostBack Then

            Exit Sub

        End If

        If Me.PreviousPage Is Nothing Then

            lbl1.Text = ""

            lbl3.Text = ""

            CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

            CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "three"
        Else

            lbl1.Text = CType(Me.PreviousPage.Master.FindControl("hfTest0"), HiddenField).Value

            lbl3.Text = CType(Me.PreviousPage.Master.FindControl("hfTest1"), HiddenField).Value

            If CType(Me.PreviousPage.Master.FindControl("hfTest0"), HiddenField).Value = "Active" Then

                CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

                CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "THREE"
            Else
                CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value = "Active"

                CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value = "three"
            End If
        End If

    lbl2.Text = CType(Me.Page.Master.FindControl("hfTest0"), HiddenField).Value

    lbl4.Text = CType(Me.Page.Master.FindControl("hfTest1"), HiddenField).Value

For the three test pages code-behind btnTest Click Events, I added the following: single lines:

Test1.aspx.vb Server.Transfer("Test2.aspx", True)

Test2.aspx.vb Server.Transfer("Test3.aspx", True)

Test3.aspx.vb Server.Transfer("Test1.aspx", True)

This makes for a testing loop wherein the page Test1 transfers to Test2 which transfers to Test3 which transfers back to Test1 all using the Button Click with the Server.Transfer.

When I start the web site at Test1, the IsPostBack is initially false and Me.PreviousPage Is Nothing as expected and displays the expected values in the page body as:

hfTest0 (Coming In):
hfTest1 (Coming In): 

btnTest BUTTON HERE with Text = "GoTo Test2"

hfTest0 (After Load): Active
hfTest2 (After Load): one

Upon clicking the btnTest on Test1, it again fires the Load Event of Test1 with IsPostBack now equal to TRUE and Me.PreviousPage Is Nothing as expected. Then it fires the load event of Test2 with IsPostBack set to FALSE and the Me.PreviousPage set to Test1 as expected and displays the expected values in the page body as:

hfTest0 (Coming In): Active
hfTest1 (Coming In): one

btnTest BUTTON HERE with Text = "GoTo Test3"

hfTest0 (After Load): Active
hfTest2 (After Load): TWO 

Upon clicking the btnTest on Test2, it fires the Load Event of Test2 with IsPostBack again equal to FALSE. I WOULD HAVE EXPECTED IT TO BE TRUE. Also the Me.PreviousPage Is Nothing. Because of the Unexpected conditions above, the resulting page display is:

hfTest0 (Coming In):
hfTest1 (Coming In): 

btnTest BUTTON HERE with Text = "GoTo Test3"

hfTest0 (After Load): Active
hfTest2 (After Load): two

Upon clicking the btnTest with it's Text Value of "GoTo Test3", it fired the Test2 Load with the IsPostback = TRUE and Me.PreviousPage of Nothing. Then it fired the Load of Test3 with the IsPostBack equal to FALSE and the Me.PreviousPage equal to Test2. The page values displayed as:

hfTest0 (Coming In): Active
hfTest1 (Coming In): two

btnTest BUTTON HERE with Text = "GoTo Test1"

hfTest0 (After Load): Active
hfTest2 (After Load): THREE

This pattern repeats itself. The desired passing of parameters is not achieved. For the page on which the btnTest is first clicked, the IsPostBack is not set to True necessitating another button click to go to the next page, but without the desired setting of the hidden fields in the Site.Master.


HERE's THE THING ABOUT THE BEHAVIOR I EXPECTED


When I recreate FROM SCRATCH this website using VS2012 using Visual Basic and the exact code above, it transfers faithfully from page to page on the first clicks setting the hidden fields as desired. I think the main problem here is that on the first transfer to another page, the IsPostBack settings work the expected way, but not for the next subsequent transfer.

Furthermore, when I run the website created in VS2013 in VS2012, it exhibits the same unexpected and undesired behavior.

Thank you for reading my long explanation. Am I missing something basic here?

Any help will be much appreciated.




Aucun commentaire:

Enregistrer un commentaire