0

我在 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 隐藏变量中?

提前致谢!!

4

1 回答 1

2

IsPostBack 是Page 类的公共属性。Daryal 对这个问题的回答解释了该类的结构。

从那个答案:

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; }
于 2016-06-17T20:20:20.187 回答