【问题标题】:Where is stored the property IsPostBack? in asp.netIsPostBack 属性存储在哪里?在 asp.net 中
【发布时间】:2016-10-19 18:21:36
【问题描述】:

我在asp.net中有这个程序

<body>
    <form id="form1" runat="server">
      <div>    
         <asp:Button runat ="server" ID="btnTest" Text ="Request Somethig"   
         OnClick ="OnClick" />
       </div>
    </form>
</body>

以及背后的代码:

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack)
    Response.Write("A Post Back has been sent from server");
   }

protected void OnClick(object sender, EventArgs e)
{          
   //The button has AutoPostBack by default
 }  

}

如果我向服务器请求页面http://localhost:50078/Default.aspx ,服务器将创建类_Default.cs的实例, 然后它会触发和事件Page_Load,并且这行不会第一次执行:

Response.Write("A Post Back has been sent from server");

原因是 IsPostBack=false

然后,如果我点击按钮,我会从服务器请求回发,所以现在 IsPostBack 将是真的,在我的浏览器中我会看到消息

"A Post Back has been sent from server"

我的问题是:属性 IsPostBack 是如何从 false 变为 true 的,该值存储在哪里?

据我所知,一旦将 HTML 发送到客户端,服务器从 _Default.cs 类创建的实例就会被销毁,因此,当我单击按钮时,它应该与 IsPostBack 属性无关(执行回帖)。

服务器是否将 IsPostback 的值存储在页面本身的 _VIEWSTATE 隐藏变量中?

提前致谢!!

【问题讨论】:

标签: asp.net ispostback


【解决方案1】:

IsPostBack 是Page class 的公共属性。 Daryal 对this question 的回答解释了该类的结构。

从那个答案:

Page 类派生自 TemplateControl 类;

public class Page : TemplateControl, IHttpHandler

并且 TemplateControl 类派生自抽象 Control 类;

public abstract class TemplateControl : Control, ...

在Page类派生的Control类中,有一个名为Page的虚拟属性;

// Summary:
//     Gets a reference to the System.Web.UI.Page instance that contains the server
//     control.
//
public virtual Page Page { get; set; }

Page 类中有 IsPostBack、IsValid 等属性;

// Summary:
//     Gets a value that indicates whether the page is being rendered for the first
//     time or is being loaded in response to a postback.
//        
public bool IsPostBack { get; }

【讨论】: