MiscellaneousUser
MiscellaneousUser

Reputation: 3043

ASP.NET IsPostBack without Page

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?

Upvotes: 5

Views: 1011

Answers (3)

John Wu
John Wu

Reputation: 52260

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);
}

Upvotes: 3

moarboilerplate
moarboilerplate

Reputation: 1643

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.

Upvotes: 2

John Wu
John Wu

Reputation: 52260

Yep

  1. Check to see if the HTTP verb is POST
  2. Check the Forms collection for a value associated with "__PREVIOUSPAGE"

See also here

Upvotes: 1

Related Questions