C# IsPostBack 实际上是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/829249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:18:56  来源:igfitidea点击:

What does IsPostBack actually mean?

c#asp.net

提问by Dead account

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

我很想知道 Page.IsPostBack 的具体含义。我完全了解它在标准 ASP.NET 页面中的日常使用,它表明用户正在将数据提交回服务器端。请参阅页面:IsPostBack 属性

But given this HTML

但鉴于这个 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.

单击提交按钮时,页面 Page_Load 方法被调用,但 Page.IsPostBack 返回 false。我不想添加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?


我已经添加了更新<input type="text" value="aa" name="ctrl" id="ctrl" />所以 Request.Form 有一个元素,Request.HTTPMethod 是 POST,但 IsPostBack 仍然是假的?

采纳答案by Allrameest

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.

实现此目的的一种方法是扩展 ASP.NET Page 类,“覆盖” IsPostBack 属性并让所有页面都从扩展页面派生。

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

回答by Fredrik M?rk

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.

在您的问题中包含的示例中,不涉及视图状态;服务器无法将此请求链接到页面的先前请求并将它们视为一个单元。导致单击按钮的请求看起来像进入服务器的任何其他随机请求。

回答by Mark Brittingham

You could check the headers to see if your inputcontrols are returning a value (using Request.Forms as tvanfosson points out). However, the really big question is why you would notwant 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.

您可以检查标题以查看您的输入控件是否正在返回值(使用 Request.Forms 作为 tvanfosson 指出)。然而,真正的大问题是,为什么你会希望添加RUNAT =服务器。ASP.NET 实现的整个页面处理大厦(MVC 除外)依赖于处理通过服务器输出的页面来设置适当的客户端代码用于回调等。

回答by tvanfosson

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.

检查 Request.Form 集合以查看它是否为非空。只有 POST 才会在 Request.Form 集合中包含数据。当然,如果没有表单数据,则请求与 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.

至于您标题中的问题,当请求是来自服务器端表单控件的 POST 时,IsPostBack 设置为 true。仅将您的表单设为客户端,就可以解决这个问题。

回答by Kris

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

通常,您可以将 PostBack 视为以下各项的组合:

  1. HTTP request method equals "POST"
  2. HTTP header HTTP_REFERER equals the current URL
  1. HTTP 请求方法等于“POST”
  2. HTTP 标头 HTTP_REFERER 等于当前 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, backto the current page.

这不是 100% 万无一失的,它没有考虑任何类型的任何状态(即使您不知道,您也可能想要),但它是一个帖子返回当前页面。