Page.IsPostBack 属性

定义

获取一个值,该值指示页是第一次呈现还是为了响应回发而加载。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:
 property bool IsPostBack { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool IsPostBack { get; }
member this.IsPostBack : bool
Public ReadOnly Property IsPostBack As Boolean

属性值

如果是为响应客户端回发而加载该页,则为 true;否则为 falsetrue if the page is being loaded in response to a client postback; otherwise, false.

属性

示例

下面的示例演示如何在加载页面时测试 IsPostBack 属性的值,以确定是第一次呈现页面还是响应回发。The following example shows how to test the value of the IsPostBack property when the page is loaded in order to determine whether the page is being rendered for the first time or is responding to a postback. 如果是第一次呈现该页,则代码将调用 Page.Validate 方法。If the page is being rendered for the first time, the code calls the Page.Validate method.

页面标记(未显示)包含 RequiredFieldValidator 控件,这些控件在未对所需输入字段进行任何输入时显示星号。The page markup (not shown) contains RequiredFieldValidator controls that display asterisks if no entry is made for a required input field. 调用 Page.Validate 会导致在呈现页面时立即显示星号,而不是等到用户单击 "提交" 按钮。Calling Page.Validate causes the asterisks to be displayed immediately when the page is rendered, instead of waiting until the user clicks the Submit button. 在回发之后,无需调用 Page.Validate,因为该方法是作为 Page 生命周期的一部分调用的。After a postback, you do not have to call Page.Validate, because that method is called as part of the Page life cycle.

private void Page_Load()
{
    if (!IsPostBack)
    {
        // Validate initially to force asterisks
        // to appear before the first roundtrip.
        Validate();
    }
}
Sub Page_Load
    If Not IsPostBack
        ' Validate initially to force the asterisks
        ' to appear before the first roundtrip.
        Validate()
    End If
End Sub

注解

有关回发和回调之间的差异的说明,请参阅不使用回发实现客户端回调For an explanation of the difference between postbacks and callbacks, see Implementing Client Callbacks Without Postbacks.

适用于

另请参阅