Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET IsPostBack without Page

Tags:

c#

asp.net

Is there a way I can find out if a Page has been posted back without using the Page object. I want to know if the page has been posted back without passing a parameter to the function, like you can check the Request object by using httpContext.Current.Request , is there a Page equivalent? The check occurs in a library function?

like image 805
MiscellaneousUser Avatar asked Jun 23 '15 20:06

MiscellaneousUser


2 Answers

Here's another technique. You can get the Page from HttpContext, and check its IsPostBack method. That way you don't have to pass the page or an IsPostBack flag to your helper function.

void MyHelperFunction()
{   
    Page page = HttpContext.Current.Handler as Page;
    bool isPostBack = (page != null) && (page.IsPostBack);
}
like image 192
John Wu Avatar answered Oct 04 '22 04:10

John Wu


If you're only trying to avoid using the Page property, and not the Page class, you can cast HttpContext.Current.Handler to a Page object in the context of a standard page request.

like image 32
moarboilerplate Avatar answered Oct 04 '22 06:10

moarboilerplate