ClientScriptManager.GetPostBackEventReference 方法

定义

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。

重载

GetPostBackEventReference(Control, String, Boolean)

返回一个在客户端事件中使用的字符串,以便回发到服务器。 该引用字符串由处理回发的指定控件和一个附加事件信息的字符串参数定义。 可选择为验证注册事件引用。

GetPostBackEventReference(PostBackOptions, Boolean)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。 由指定的 PostBackOptions 对象定义的字符串。 可选择为验证注册事件引用。

GetPostBackEventReference(PostBackOptions)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。 由指定的 PostBackOptions 实例定义的字符串。

GetPostBackEventReference(Control, String)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。 该引用字符串由处理回发的指定控件和一个附加事件信息的字符串参数定义。

GetPostBackEventReference(Control, String, Boolean)

返回一个在客户端事件中使用的字符串,以便回发到服务器。 该引用字符串由处理回发的指定控件和一个附加事件信息的字符串参数定义。 可选择为验证注册事件引用。

public string GetPostBackEventReference (System.Web.UI.Control control, string argument, bool registerForEventValidation);

参数

control
Control

在服务器上处理回发的服务器 Control

argument
String

传递给 control 的可选参数的字符串。

registerForEventValidation
Boolean

true 表示为验证注册事件引用;否则为 false

返回

一个字符串,将其视为客户端脚本时,将启动回发。

例外

指定的 Controlnull

注解

若要实现 IPostBackEventHandlerPage接口,请使用 @ Implements 指令。

GetPostBackEventReference 属性为 falseUseSubmitBehaviorButton 方法可与 控件一起使用。 在此方案中, GetPostBackEventReference 方法返回 控件的客户端回发事件 Button

如果 registerForEventValidation 为 true,则 GetPostBackEventReference(PostBackOptions, Boolean) 该方法调用 RegisterForEventValidation(String, String) 方法以使用表示正在生成事件的客户端控件的唯一控件 ID 注册事件引用进行验证。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

GetPostBackEventReference(PostBackOptions, Boolean)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。 由指定的 PostBackOptions 对象定义的字符串。 可选择为验证注册事件引用。

public string GetPostBackEventReference (System.Web.UI.PostBackOptions options, bool registerForEventValidation);

参数

options
PostBackOptions

定义回发的 PostBackOptions

registerForEventValidation
Boolean

true 表示为验证注册事件引用;否则为 false

返回

一个字符串,当被视为客户端脚本时,它将启动客户端回发。

例外

注解

若要实现 IPostBackEventHandlerPage 对象的 接口,请使用 @ Implements 指令。

GetPostBackEventReference 属性为 falseUseSubmitBehaviorButton 方法可与 控件一起使用。 在此方案中, GetPostBackEventReference 方法返回 控件的客户端回发事件 Button

如果 registerForEventValidationtrue,则 GetPostBackEventReference(PostBackOptions, Boolean) 方法调用 RegisterForEventValidation(String, String) 方法以注册事件引用以使用唯一控件 ID 进行验证,该 ID 表示正在生成事件的客户端控件。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

GetPostBackEventReference(PostBackOptions)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。 由指定的 PostBackOptions 实例定义的字符串。

public string GetPostBackEventReference (System.Web.UI.PostBackOptions options);

参数

options
PostBackOptions

定义回发的 PostBackOptions

返回

一个字符串,当被视为客户端脚本时,它将启动客户端回发。

例外

注解

若要实现 IPostBackEventHandlerPage接口,请使用 @ Implements 指令。

GetPostBackEventReference 属性为 falseUseSubmitBehaviorButton 方法可与 控件一起使用。 在此方案中, GetPostBackEventReference 方法返回 控件的客户端回发事件 Button

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

GetPostBackEventReference(Control, String)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。 该引用字符串由处理回发的指定控件和一个附加事件信息的字符串参数定义。

public string GetPostBackEventReference (System.Web.UI.Control control, string argument);

参数

control
Control

在服务器上处理回发的服务器 Control

argument
String

要传递给处理回发的控件的可选参数字符串。

返回

一个字符串,将其视为客户端脚本时,将启动回发。

例外

指定的 Controlnull

示例

下面的代码示例演示如何使用 GetPostBackEventReference 方法。 自定义控件 MyControl实现 IPostBackEventHandler 接口。 单击页面上的按钮时, RaisePostBackEvent 将调用自定义控件的 方法。

<%@ Page Language="C#"  %>

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

<script runat="server">

  public class MyControl : Label, IPostBackEventHandler
  {

    // Use the constructor to defined default label text.
    public MyControl()
    {
      base.Text = "No postback raised.";
    }
    
    // Implement the RaisePostBackEvent method from the
    // IPostBackEventHandler interface. 
    public void RaisePostBackEvent(string eventArgument)
    {
      base.Text = "Postback handled by " + this.ID.ToString() + ". <br/>" +
                  "Postback caused by " + eventArgument.ToString() + ".";
      
    }
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Create an instance of the custom label control and 
    // add it to the page.
    MyControl mycontrol = new MyControl();
    mycontrol.ID = "mycontrol1";
    PlaceHolder1.Controls.Add(mycontrol);
    PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    
    // Create a button element with its onClick attribute defined
    // to create a postback event reference to the custom label control.
    HtmlInputButton b = new HtmlInputButton();
    b.ID = "mybutton1";
    b.Value = "Click";
    b.Attributes.Add("onclick", cs.GetPostBackEventReference(mycontrol, b.ID.ToString()));
    PlaceHolder1.Controls.Add(b);
    PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    
    // Create a link element with its href attribute defined
    // to create a postback event reference to the custom label control.
    HtmlAnchor a = new HtmlAnchor();
    a.ID = "myanchor1";
    a.InnerText = "link";
    a.HRef = cs.GetPostBackClientHyperlink(mycontrol, a.ID.ToString());
    PlaceHolder1.Controls.Add(a);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ClientScriptManager Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:PlaceHolder id="PlaceHolder1" 
                       runat="server">
      </asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

下面的代码示例具有与上一个代码示例相同的功能,只不过类实现 IPostBackEventHandler 接口而不是自定义控件Page

<%@ Page Language="C#"  %>
<%@ Implements Interface="System.Web.UI.IPostBackEventHandler" %>

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

<script runat="server">
  
  public void RaisePostBackEvent(string eventArgument)
  {
    Label1.Text = "Postback handled by " + this.ID.ToString() + ". <br/>" +
                  "Postback caused by " + eventArgument.ToString() + "."; ;
  }
  
  protected void Page_Load(object sender, EventArgs e)
  {
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;
    
    // Create a button element with its onClick attribute defined
    // to create a postback event reference to the custom label control.
    HtmlInputButton b = new HtmlInputButton();
    b.ID = "mybutton1";
    b.Value = "Click";
    b.Attributes.Add("onclick", cs.GetPostBackEventReference(this, b.ID.ToString()));
    PlaceHolder1.Controls.Add(b);
    PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
    
    // Create a link element with its href attribute defined
    // to create a postback event reference to the custom label control.
    HtmlAnchor a = new HtmlAnchor();
    a.ID = "myanchor1";
    a.InnerText = "link";
    a.HRef = cs.GetPostBackClientHyperlink(this, a.ID.ToString());
    PlaceHolder1.Controls.Add(a);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ClientScriptManager Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Label1"
                 runat="server" />
      <br />
      <asp:PlaceHolder id="PlaceHolder1" 
                       runat="server">
      </asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

注解

若要实现 IPostBackEventHandlerPage接口,请使用 @ Implements 指令。

GetPostBackEventReference 属性为 falseUseSubmitBehaviorButton 方法可与 控件一起使用。 在此方案中, GetPostBackEventReference 方法返回 控件的客户端回发事件 Button

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1