Thursday, June 22, 2006

 

Lightly crossing pages

The Server.Transfer method is an efficient way to transition between pages under server control, but it transmits no parameters to specify a context. There are indirect ways to do this with added costs in performance and data management. However, the C# language provides data transfer with no added programming or overhead, provided the destination page knows the type of the origin page. Simply declare and assign public members in the origin page and use their values in the destination page. This is made possible by the PreviousPage property of the Page class, the strong typing of C# and the run-time type identification system.

In a Login page, for example, declare a member such as m_ilUserTag to identify a validated user:

public partial class Login : System.Web.UI.Page
{
    public long m_ilUserTag = 0;

Once a user is validated, assign an identifier value to m_ilUserTag and transfer to the Home page:

        Server.Transfer("Home.aspx");

The Home page can pick up the value of m_ilUserTag directly. For example:

    protected void Page_Load(object sender, EventArgs e)
    {
        object oObject = (object)PreviousPage;
        Login oLoginPage = (Login)oObject;
        long ilUserTag = oLoginPage.m_ilUserTag;

Although you might think that the reference to the Page base class furnished by PreviousPage could be cast immediately to the Login derived class, Visual Studio 2005 will bark at that with a diagnostic such as "Error...Cannot convert type 'System.Web.UI.Page' to 'System.Web.UI.WebControls.Login'." If you pass the reference through the type, object, it's happy. RTTI will check validity of the cast and throw an exception if PreviousPage did not reference a Login object.

Comments: Post a Comment

Subscribe to Post Comments [Atom]



Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]