What does IsPostBack actually mean?

I am interested to know what specifically Page.IsPostBack means. I am fully aware of it’s day to day use in a standard ASP.NET page, that it indicates that the user is
submitting data back to the server side. See Page:IsPostBack Property

But given this HTML

<html>
   <body>
      <form method="post" action="default.aspx">
         <input type="submit" value="submit" />
      </form>
   </body>
</html>

When clicking on the Submit button, the pages Page_Load method is invoked, but the Page.IsPostBack is returning false. I don’t want to add runat=server.

How do I tell the difference between the pages first load, and a Request caused by the client hitting submit?

update
I’ve added in <input type="text" value="aa" name="ctrl" id="ctrl" /> so the Request.Form has an element, and Request.HTTPMethod is POST, but IsPostBack is still false?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Check the Request.Form collection to see if it is non-empty. Only a POST will have data in the Request.Form collection. Of course, if there is no form data then the request is indistinguishable from a GET.

As to the question in your title, IsPostBack is set to true when the request is a POST from a server-side form control. Making your form client-side only, defeats this.

Method 2

One way to do this is to extend the ASP.NET Page class, “override” the IsPostBack property and let all your pages derive from the extended page.

public class MyPage : Page
{
    public new bool IsPostBack
    {
        get 
        { 
          return 
            Request.Form.Keys.Count > 0 &&
            Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase); 
         }
    }
}

Method 3

In the example that you include in your question, there is no viewstate involved; there is no way for the server to link this request to a previous request of the page and treat them as a unit. The request resulting in clicking the button will look like any other random request coming in to the server.

Method 4

Generally a you could view a PostBack as a combination of:

  1. HTTP request method equals “POST”
  2. HTTP header HTTP_REFERER equals the current URL

That’s not 100% foolproof tho, it does not take into account any state of any kind (which you probably want even if you don’t know it) but it is a post, back to the current page.

Method 5

You could check the headers to see if your input controls are returning a value (using Request.Forms as tvanfosson points out). However, the really big question is why you would not want to add runat=server. The entire page processing edifice implemented by ASP.NET (except MVC) depends on processing the page output through the server to set up the appropriate client-side code for callbacks, etc.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x