cross page posting not working

Had some fun and games recently with the cross page posting facility in 2. Consider this code snippet on the page that is being posted to (page2.aspx):

If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
  Dim str As String = ""
  Dim tmpTextBox As TextBox = CType(PreviousPage.FindControl("TextBox_1"), TextBox)

  If tmpTextBox IsNot Nothing Then
   str = tmpTextBox.Text
  End If

End If

I was expecting it to work....but no.

So tried this method instead:

Added this directive to page2.aspx

<%@ PreviousPageType VirtualPath="~/page1.aspx" %>

Added a public read only property to page1.aspx

Public ReadOnly Property myTextBox() As TextBox
   Return Me.TextBox_1
  End Get
End Property

Then changed the code on page2.aspx to this:

Dim str As String = ""
If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
  str= PreviousPage.myTextBox.Text
End If

This method works..Whats confusing is that the first method should also work. Currently thinking that this phenomenon is happening as it was in a website rather than a web app - so the previous page need to be more strongly tied to the posted to page due to the way that its all compiled. Interestingly adding the PreviousPageType directive and using the original code still doesn't work, so the key is the creation of the public property in page1.aspx.

Post a comment