How to determine the SERVER.TRANSFER'ing aspx page.

Hello,

I'm wanting to know how to determine the page that was just transferred away from. For example given three web pages; CallerA.aspx, CallerB.aspx, and TargetC.aspx. Both CallerA and CallerB can perfrom a 'Server.Transfer' to TargetC. TargetC.aspx which needs to appear differently based on where the transfer came from. TargetC also uses properties from the calling page in order to retrieve information from that page.

In VB2003 we used code such as this:

IfNot IsPostBackThen
IfTypeOf (Context.Handler)Is CallerAThen
'Get a reference to the previous page's class (CallerA.aspx)
oPage =CType(Context.Handler, ProjectName.CallerA)
'and retrieve information from previous page's properties
sInformation = oPage.EmployeeInfo
Me.txtBoxInfo.Text = sInformation
EndIf
End if

IfNot IsPostBackThen
IfTypeOf (Context.Handler)Is CallerBThen
'Get a reference to the previous page's class (CallerB.aspx)
oPage =CType(Context.Handler, ProjectName.CallerB)
'and retrieve information from previous page's properties
sInformation = oPage.ManagerInfo
Me.txtBoxInfo.Text = sInformation
EndIf
End if

So I've really got two questions:

1. How do I determine the page that just made the call.
2. How do I pass information between pages.

Thanks in advance

[3034 byte] By [tbailey] at [2007-12-16]
# 1
These can probably be answered together.
If you simply use the session state you can set a variable to the name of the transferring page in then transfer
eg.


Session("last_page") = "CallerA.aspx"
Server.Transfer("TargetC.aspx")


then retrieve the value on the target page


Response.Write(Session("last_page"))


this can also be used for any other information you wish to pass to the target.
hope this helps
exhibit at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Thanks so much for a workable solution. Yes, using the session state would indeed resolve the issue. My problem with this solution is that we already have a TON of pages setup with properties as our methodology of passing the information between pages. In addition to passing info between pages, we also use the classes outside of page navigation as well.

I’m thinking there must be a way to get a reference via the new HttpContext object that will let us do the same type operation we used to utilize. We know we’re going to have to retrofit our application to work in VB 2005. So we could change all of our pages to pass via the session, but it would be large undertaking. Hopefully someone knows a technique to use the new object model in a manner that will minimize the volume of retrofit required.

tbailey at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Well after MUCH digging I have found a solution that fits our needs. Here is how I'm going to proceed.

Dim sPageName As String = "Unknown"
Dim sInformation As String = "None found"

'Get a reference to the previous page's class
Dim oPage As Object
oPage = Me.PreviousPage

sPageName = oPage.ToString

If Not IsPostBack Then
If sPageName = "ASP.CallerA_aspx" Then
'We have a reference to (CallerA.aspx) and can
'retrieve information from previous page's properties
sInformation = oPage.EmployeeInfo
Me.txtBoxInfo.Text = sInformation
End If
End If

If Not IsPostBack Then
If sPageName = "ASP.CallerB_aspx" Then
'We have a reference to (CallerB.aspx) and can
'retrieve information from previous page's properties
sInformation = oPage.ManagerInfo
Me.txtBoxInfo.Text = sInformation
End If
End If

Thanks for the help.

tbailey at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
You may find this code easier when you get more pages to sort through...



If Not IsPostBack Then
Select Case Me.PreviousPage.ToString
Case "ASP.CallerA_aspx"
Me.txtBoxInfo.Text = Me.PreviousPage.EmployeeInfo
Case "ASP.CallerB_aspx"
Me.txtBoxInfo.Text = Me.PreviousPage.sInformation
Case Else
'Uknown page
Me.txtBoxInfo.Text ="Uknown Page"
End
Select
End If



Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Dustin,

Good suggestion of using the Select Case statement. We are going to use this. However, I can't figure out a way around the using an OJBECT variable to hold the previous page. When I tried to use your syntax "Me.PreviousPage.EmployeeInfo" I get a compile error.

Error 1 C:\Documents and Settings\tbailey\My Documents\Visual Studio 2005\WebSites\WebSite1\TargetC.aspx.vb(39): error BC30456: 'EmployeeInfo' is not a member of 'System.Web.UI.Page'. /

The 'PreviousPage' property is returning an ASP.NET Page object. Of course the general page object doesn't have my custome properties present. I've tried to dim the page object to the class of the calling page, but have not had any luck. So we are currently using the following:

Dim sPageName As String = "Unknown"

Dim oPage As Object '<== Would prefer to avoid this, but we can't declare
'Dim oPage As CallerA <== a variable like this no matter how the previous page's
module was scoped!! We have tried PUBLIC, PARTIAL,
PARTIAL PUBLIC and none seem to allow a reference.

oPage =
Me.PreviousPage
sPageName = oPage.ToString

Select Case sPageName
Case "ASP.CallerA_aspx"
Me.Textbox.Text = Me.PreviousPage.EmployeeInfo
Case "ASP.CallerB_aspx"
Me.Textbox.Text = Me.PreviousPage.ManagerInfo
Case Else
Err.Raise(2000, "Invalid page", "Navigation to this page is an error.")
End Select

Any suggestions on how to get typed reference to the calling page's class? This would prevent the late bound call to a OBJECT variable.

Thanks much

tbailey at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Hi,

If the class of the calling page can differ, then you have to declare as object because you can't be sure it's going to be of a certain class.

You can get around it by declaring all the class types you can send through and setting to nothing. IE. Dim oPage as CallerA=Nothing
and so on for all the other classes. Then set the variable accordingly using CType()

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Dustin,

I really appreciate you offering solutions. Based on your suggestions, I've finally found a way to 'kind of' do what I'm hoping. Here's the code.



Dim sPageName As String = "Unknown"
Dim Propertyvalue As String = "None found"

sPageName = Me.PreviousPage.ToString

Select Case sPageName
Case "ASP.CallerA_aspx"
Dim oPage As CallerA = PreviousPage
Propertyvalue = oPage.EmployeeInfo
Case "ASP.CallerB_aspx"
Dim oPage As CallerB = PreviousPage
Propertyvalue = oPage.ManagerInfo
Case Else
Err.Raise(2000, "Invalid page", "Navigation to this page has created an error.")
End
Select

Me.DisplayLabel.Text = "Previous Page {" & sPageName & "}"
Me.InformationTextbox.Text = Propertyvalue

I say 'kind of' because the IDE still isn't recognizing CallerA/CallerB as class definitions. The IDE puts a blue squiggly under both the CallerA/CallerB and complains that "Type 'CallerA' is undefined".

The project compiles, runs, and functions property. But the experience is like there isn't any early binding occurring. At least design time references aren't getting resolved. We don't get any intellisense about the public properties 'EmployeeInfo' or 'ManagerInfo'.

I guess we are going to go with this technique and hope that when the released version of VS comes out later this year the referencing problem behaves better.


Thanks again for the help,
Trey

tbailey at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...