Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does IsPostBack actually mean?

Tags:

c#

asp.net

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?

like image 561
Dead account Avatar asked May 06 '09 12:05

Dead account


People also ask

What is a postback?

A Postback is an action taken by an interactive webpage, when the entire page and its contents are sent to the server for processing some information and then, the server posts the same page back to the browser. @Galwegian: Don't you think then it will only happen when some validation errors occur so as to present the same page as the outcome.

What is the meaning of ispostback=false?

As the property name implies, isPostback==false means the page has not been posted back----------> means the page is loading for the first time. So when firsttime you are requesting for the page isPostback=false at that point of time. If you pree refresh button, this also means you are requesting for the page for the first time.

When is ispostback set to true?

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.

What is the use of ispostback event in Salesforce?

Is Postback is normally used on page _load event to detect if the web page is getting generated due to postback requested by a control on the page or if the page is getting loaded for the first time. C# Source Code


4 Answers

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.

like image 56
tvanfosson Avatar answered Sep 21 '22 18:09

tvanfosson


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); 
         }
    }
}
like image 30
Allrameest Avatar answered Sep 19 '22 18:09

Allrameest


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.

like image 26
Fredrik Mörk Avatar answered Sep 23 '22 18:09

Fredrik Mörk


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.

like image 36
Kris Avatar answered Sep 22 '22 18:09

Kris