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:

publicoverride String ID

{

get

{

String id = null;

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

{

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

}

elseif (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);

}


}

protectedoverridevoid OnPreInit(EventArgs e)

{

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

}

If you have better solution, just tell me !

0 comments:

Post a Comment