Generating a Unique Identifier for Each Page Instance

Sometimes there may be a need for identifying each page instance univocally. One ocasion may be, for example, if we want to store something in a session which is specific to a certain page.

What I did was override the Page class' ID property and have it return a unique value for a page that is kept throughout all postbacks. Here is my code:

public override String ID

{

 

    get

    {

        String id = null;        if ((this.IsPostBack == true) && (String.IsNullOrEmpty(this.Context.Request.Form["__ID"]) == false))

        {            id = this.Context.Request.Form["__ID"];

        }

        else if (String.IsNullOrEmpty(this.Context.Items [ "__ID" ] as String) == false)

        {

             id = this.Context.Items [ "__ID" ] as String;

        }

        else

        {

            id = Guid.NewGuid().ToString();            this.Context.Items [ "__ID" ] = id;

        }

 

        return (id);

    }

}

protected override void OnPreInit(EventArgs e)

{

    this.ClientScript.RegisterHiddenField("__ID", this.ID);    base.OnPreInit(e);

}

Bookmark and Share

                             

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website