Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access current instance of Page from a static class

Tags:

static

asp.net

Basic question - is it possible to access the current Page from a static class in ASP.NET?

I am thinking no, as google turns up no results.

like image 665
maxp Avatar asked Mar 22 '10 19:03

maxp


People also ask

How do I find the instance of a static class?

You can not return an instance of a static class as a static class by definition can not be instantiated. The singleton pattern just makes sure that you always return the same instance of a class. But said class can never be static.

Can you instance a static class?

A static class cannot be instantiated. All members of a static class are static and are accessed via the class name directly, without creating an instance of the class. The following code is an example of a static class, CSharpCorner. We know that all members of the class are static.

How can I view ViewState in static method?

Create a class conteining the properties of the viewState you want to access to. In the constructor pass the real ViewState. Create a static instance of the class but not initialize it. In the PageLoad initialize Not static class and the static one.

How can we use session in static method?

Just use the below syntax: HttpContext. Current. Session["SessionName"].


1 Answers

Technically you could just get the current IHttpHandler for the request. Since Page implements that, then you could check to see if it is one.

var page = HttpContext.Current.CurrentHandler as Page;

if(page != null){
    // Do something with page
}
like image 79
Josh Avatar answered Oct 24 '22 09:10

Josh