PostBackOptions Class

Definition

指定如何生成客户端 JavaScript 以启动回发事件。Specifies how client-side JavaScript is generated to initiate a postback event.

public ref class PostBackOptions sealed
public sealed class PostBackOptions
type PostBackOptions = class
Public NotInheritable Class PostBackOptions
Inheritance
PostBackOptions

Examples

下面的代码示例使用 Button 控件的 OnClick 事件为 HyperLink 控件生成客户端脚本,该脚本将允许 HyperLink 控件引发回发事件。The following code example uses the OnClick event of a Button control to generate client-side script for a HyperLink control that will allow the HyperLink control to cause a postback event. 由于 myPostBackOptions 对象的 ActionUrl 属性设置为 "Page2",因此回发会将 Web 窗体页发布到名为 "Page2" 的另一页,此处未提供此页。Because the ActionUrl property of the myPostBackOptions object is set to "Page2.aspx", the postback will post the Web Forms page to another page named "Page2.aspx", which is not provided here. 若要使用此示例,必须将名为 "Page2" 的另一个网页添加到项目或目录中。To use this example, you must add another Web page named "Page2.aspx" to your project or directory.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class postbackoptionscs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Label1.Text = "A postback event has occurred.";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Create a new PostBackOptions object and set its properties.
        PostBackOptions myPostBackOptions = new PostBackOptions(this);
        myPostBackOptions.ActionUrl = "Page2.aspx";
        myPostBackOptions.AutoPostBack = false;
        myPostBackOptions.RequiresJavaScriptProtocol = true;
        myPostBackOptions.PerformValidation = true;

        // Add the client-side script to the HyperLink1 control.
        HyperLink1.NavigateUrl = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);

        Label1.Text = "Click this hyperlink to initiate a postback event.";
    }
}
Partial Class postbackoptionsvb
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        ' Create a new PostBackOptions object and set its properties.
        Dim myPostBackOptions As PostBackOptions = New PostBackOptions(Me)
        myPostBackOptions.ActionUrl = "Page2.aspx"
        myPostBackOptions.AutoPostBack = False
        myPostBackOptions.RequiresJavaScriptProtocol = True
        myPostBackOptions.PerformValidation = True

        ' Add the client-side script to the HyperLink1 control.
        HyperLink1.NavigateUrl = Page.ClientScript.GetPostBackEventReference(myPostBackOptions)

        Label1.Text = "Click this hyperlink to initiate a postback event."

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack Then
            Label1.Text = "A postback event has occurred."
        End If

    End Sub
End Class

下面的代码示例是一个可用于运行前面的代码隐藏文件的网页。The following code example is a Web page that can be used to run the preceding code-behind file.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="postbackoptions.aspx.cs" Inherits="postbackoptionscs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>PostBackOptions Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <h3>PostBackOptions Example Page</h3>
      Click this button to create client-side script for the 
      Postback hyperlink that causes a postback event to occur.
      <br />
      <asp:Button id="Button1" 
        runat="server" 
        text="Create Script" 
        onclick="Button1_Click" />
      <br /><br />
      <asp:Label id="Label1" 
        runat="server" 
        text="">
      </asp:Label>
      <br />
      <asp:HyperLink id="HyperLink1" 
        runat="server" 
        text="Postback">
      </asp:HyperLink>
    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="postbackoptions.aspx.vb" Inherits="postbackoptionsvb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>PostBackOptions Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <h3>PostBackOptions Example Page</h3>
      Click this button to create client-side script for the 
      Postback hyperlink that causes a postback event to occur.
      <br />
      <asp:Button id="Button1" 
        runat="server" 
        text="Create Script" 
        onclick="Button1_Click" />
      <br /><br />
      <asp:Label id="Label1" 
        runat="server" 
        text=""></asp:Label>
      <br />
      <asp:HyperLink id="HyperLink1" 
        runat="server" 
        text="Postback"></asp:HyperLink>
      <br />
    </form>
  </body>
</html>

Remarks

PostBackOptions 类允许控件发出启动回发事件的客户端脚本。The PostBackOptions class allows controls to emit client-side script that initiates a postback event. PostBackOptions 类还通过 TargetControl 属性提供对启动回发事件的控件的引用。The PostBackOptions class also provides a reference to the control that initiated the postback event through the TargetControl property. 回发事件是根据传入到 ClientScriptManager.GetPostBackEventReference 方法的 PostBackOptions 对象中指定的选项创建的。The postback event is created based on the options specified in the PostBackOptions object passed in to the ClientScriptManager.GetPostBackEventReference method.

通常,通过 "提交" 按钮或 "图像" 按钮等元素启动到服务器的回发。Normally, a postback to the server is initiated by elements such as a Submit button or an Image button. 但是,通过发出客户端 JavaScript,不同的控件可以启动回发事件。However, by emitting client-side JavaScript, different controls can initiate a postback event.

Constructors

PostBackOptions(Control)

用指定的目标控件数据初始化 PostBackOptions 类的新实例。Initializes a new instance of the PostBackOptions class with the specified target control data.

PostBackOptions(Control, String)

用指定的目标控件和参数数据初始化 PostBackOptions 类的新实例。Initializes a new instance of the PostBackOptions class with the specified target control and argument data.

PostBackOptions(Control, String, String, Boolean, Boolean, Boolean, Boolean, Boolean, String)

用实例属性的指定值初始化 PostBackOptions 类的新实例。Initializes a new instance of the PostBackOptions class with the specified values for the instance's properties.

Properties

ActionUrl

获取或设置 Web 窗体页回发的目标 URL。Gets or sets the target URL for the postback of a Web Forms page.

Argument

获取或设置回发事件中传输的可选参数。Gets or sets an optional argument that is transferred in the postback event.

AutoPostBack

获取或设置一个值,该值指示窗体是否会为响应用户操作而自动回发到服务器。Gets or sets a value that indicates whether the form will automatically post back to the server in response to a user action.

ClientSubmit

获取或设置一个值,该值指示回发事件是否应从客户端脚本发生。Gets or sets a value indicating whether the postback event should occur from client-side script.

PerformValidation

获取或设置一个值,该值指示在回发事件发生之前是否需要在客户端进行验证。Gets or sets a value indicating whether client-side validation is required before the postback event occurs.

RequiresJavaScriptProtocol

获取或设置一个值,该值指示是否为客户端脚本生成了 javascript: 前缀。Gets or sets a value indicating whether the javascript: prefix is generated for the client-side script.

TargetControl

获取接收回发事件的目标控件。Gets the control target that receives the postback event.

TrackFocus

获取或设置一个值,该值指示回发事件是否应将页返回到当前的滚动位置并将焦点返回到当前控件。Gets or sets a value indicating whether the postback event should return the page to the current scroll position and return focus to the current control.

ValidationGroup

获取或设置一个控件组,当该控件组回发到服务器时,PostBackOptions 将引发对它的验证。Gets or sets the group of controls for which the PostBackOptions object causes validation when it posts back to the server.

Methods

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also