Wednesday, March 14, 2012

Use PreviousPageType to Simplify Page Data Access

In ASP.NET you can request another page either through cross-page posting, or through a Server.Transfer. You can then access the previous page and read accessible page properties.


However, the PreviousPage property returns a Page object; so, to access this property in the target page, you need to cast the PreviousPage property to the previous page's type.



You can pass that information onto ASP.NET by adding the PreviousPageType directive to the aspx file of the target page. This allows ASP.NET to strongly type the PreviousPage property in the target page so no need for casting.



The PreviousPageType directive can be added to the asp.net page to be like:
<%@ PreviousPageType VirtualPath="~/Hotel.aspx" %>


and in the source code you could access HotelId property in Hotel.aspx page by writing:
long hotelId = PreviousPage.HotelId;


Read more:
http://msdn.microsoft.com/en-us/library/ms228169.aspx 



Hope it helps all of you :)