共用方式為


ClientScriptManager.GetPostBackEventReference 方法

定義

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。

多載

GetPostBackEventReference(Control, String, Boolean)

傳回字串,以用於用戶端事件中造成回傳給伺服器。 參考字串是由處理回傳的指定控制項和其他事件資訊的字串引數所定義。 或者,註冊事件參考以進行驗證。

GetPostBackEventReference(PostBackOptions, Boolean)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。 指定的 PostBackOptions 物件會定義參考字串。 或者,註冊事件參考以進行驗證。

GetPostBackEventReference(PostBackOptions)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。 指定的 PostBackOptions 執行個體會定義參考字串。

GetPostBackEventReference(Control, String)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。 參考字串是由處理回傳的指定控制項和其他事件資訊的字串引數所定義。

GetPostBackEventReference(Control, String, Boolean)

傳回字串,以用於用戶端事件中造成回傳給伺服器。 參考字串是由處理回傳的指定控制項和其他事件資訊的字串引數所定義。 或者,註冊事件參考以進行驗證。

public:
 System::String ^ GetPostBackEventReference(System::Web::UI::Control ^ control, System::String ^ argument, bool registerForEventValidation);
public string GetPostBackEventReference (System.Web.UI.Control control, string argument, bool registerForEventValidation);
member this.GetPostBackEventReference : System.Web.UI.Control * string * bool -> string
Public Function GetPostBackEventReference (control As Control, argument As String, registerForEventValidation As Boolean) As String

參數

control
Control

在伺服器上處理回傳的伺服器 Control

argument
String

要傳遞至 control 的選擇性引數字串。

registerForEventValidation
Boolean

true 表示註冊事件參考以進行驗證,否則為 false

傳回

視為用戶端上的指令碼時,啟始回傳的字串。

例外狀況

指定的 Controlnull

備註

若要實作 IPostBackEventHandlerPage介面,請使用 @ Implements 指示詞。

GetPostBackEventReference 屬性為 falseUseSubmitBehavior,方法可以與控件搭配Button使用。 在此案例中 GetPostBackEventReference ,方法會傳回 控件的 Button 用戶端回傳事件。

如果 registerForEventValidation 為 true,方法 GetPostBackEventReference(PostBackOptions, Boolean) 會呼叫 RegisterForEventValidation(String, String) 方法,以註冊事件參考,以使用唯一的控件標識碼進行驗證,代表產生事件的用戶端控制件。

另請參閱

適用於

GetPostBackEventReference(PostBackOptions, Boolean)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。 指定的 PostBackOptions 物件會定義參考字串。 或者,註冊事件參考以進行驗證。

public:
 System::String ^ GetPostBackEventReference(System::Web::UI::PostBackOptions ^ options, bool registerForEventValidation);
public string GetPostBackEventReference (System.Web.UI.PostBackOptions options, bool registerForEventValidation);
member this.GetPostBackEventReference : System.Web.UI.PostBackOptions * bool -> string
Public Function GetPostBackEventReference (options As PostBackOptions, registerForEventValidation As Boolean) As String

參數

options
PostBackOptions

定義回傳的 PostBackOptions

registerForEventValidation
Boolean

true 表示註冊事件參考以進行驗證,否則為 false

傳回

視為用戶端上的指令碼時,啟始用戶端回傳的字串。

例外狀況

備註

若要實 IPostBackEventHandlerPage 對象的介面,請使用 @ Implements 指示詞。

GetPostBackEventReference 屬性為 falseUseSubmitBehavior,方法可以與控件搭配Button使用。 在此案例中 GetPostBackEventReference ,方法會傳回 控件的 Button 用戶端回傳事件。

如果 為 registerForEventValidationtrue,則 GetPostBackEventReference(PostBackOptions, Boolean) 方法會呼叫 RegisterForEventValidation(String, String) 方法,以註冊事件參考,以使用唯一的控件標識符進行驗證,代表產生事件的用戶端控件。

另請參閱

適用於

GetPostBackEventReference(PostBackOptions)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。 指定的 PostBackOptions 執行個體會定義參考字串。

public:
 System::String ^ GetPostBackEventReference(System::Web::UI::PostBackOptions ^ options);
public string GetPostBackEventReference (System.Web.UI.PostBackOptions options);
member this.GetPostBackEventReference : System.Web.UI.PostBackOptions -> string
Public Function GetPostBackEventReference (options As PostBackOptions) As String

參數

options
PostBackOptions

定義回傳的 PostBackOptions

傳回

視為用戶端上的指令碼時,啟始用戶端回傳的字串。

例外狀況

備註

若要實作 IPostBackEventHandlerPage介面,請使用 @ Implements 指示詞。

GetPostBackEventReference 屬性為 falseUseSubmitBehavior,方法可以與控件搭配Button使用。 在此案例中 GetPostBackEventReference ,方法會傳回 控件的 Button 用戶端回傳事件。

另請參閱

適用於

GetPostBackEventReference(Control, String)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。 參考字串是由處理回傳的指定控制項和其他事件資訊的字串引數所定義。

public:
 System::String ^ GetPostBackEventReference(System::Web::UI::Control ^ control, System::String ^ argument);
public string GetPostBackEventReference (System.Web.UI.Control control, string argument);
member this.GetPostBackEventReference : System.Web.UI.Control * string -> string
Public Function GetPostBackEventReference (control As Control, argument As String) As String

參數

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>
<%@ Page Language="VB" %>

<!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
    Inherits Label
    Implements IPostBackEventHandler
     
    Public Sub New()
      
      MyBase.Text = "No postback raised."
    
    End Sub
    
    Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
      
      MyBase.Text = "Postback handled by " & Me.ID.ToString() & ". <br/>" & _
                "Postback caused by " + eventArgument.ToString() & "."

    End Sub
    
  End Class
   

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

    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript

    ' Create an instance of the custom label control and 
    ' add it to the page.
    Dim mycontrol As 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.
    Dim b As 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.
    Dim a As New HtmlAnchor()
    a.ID = "myanchor1"
    a.InnerText = "link"
    a.HRef = cs.GetPostBackClientHyperlink(MyControl, a.ID.ToString())
    PlaceHolder1.Controls.Add(a)
    
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" 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>

下列程式代碼範例的功能與上述相同,不同之處在於類別 Page 會實作 IPostBackEventHandler 介面,而不是自定義控件。

<%@ 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>
<%@ Page Language="VB" %>
<%@ 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 Sub RaisePostBackEvent(ByVal eventArgument As String) _
    Implements IPostBackEventHandler.RaisePostBackEvent
    
    Label1.Text = "Postback handled by " & Me.ID.ToString() & ". <br/>" & _
           "Postback caused by " + eventArgument.ToString() & "."
  
  End Sub
  

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

    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript
    
    ' Create a button element with its onClick attribute defined
    ' to create a postback event reference to the custom label control.
    Dim b As New HtmlInputButton()
    b.ID = "mybutton1"
    b.Value = "Click"
    b.Attributes.Add("onclick", cs.GetPostBackEventReference(Me, 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.
    Dim a As New HtmlAnchor()
    a.ID = "myanchor1"
    a.InnerText = "link"
    a.HRef = cs.GetPostBackClientHyperlink(Me, a.ID.ToString())
    PlaceHolder1.Controls.Add(a)
    
  End Sub
</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 屬性為 falseUseSubmitBehavior,方法可以與控件搭配Button使用。 在此案例中 GetPostBackEventReference ,方法會傳回 控件的 Button 用戶端回傳事件。

另請參閱

適用於