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.
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.
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.
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.
Just use the below syntax: HttpContext. Current. Session["SessionName"].
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With