What does IsPostBack actually mean?

后端 未结 5 662
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 09:37

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 sub

相关标签:
5条回答
  • 2021-01-05 10:07

    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.

    0 讨论(0)
  • 2021-01-05 10:09

    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); 
             }
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 10:23

    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.

    0 讨论(0)
  • 2021-01-05 10:24

    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.

    0 讨论(0)
提交回复
热议问题