ClientScriptManager.GetPostBackClientHyperlink 方法

定义

获取一个引用,并将 javascript: 追加到引用的开始处,用于在客户端事件中回发到服务器。

重载

GetPostBackClientHyperlink(Control, String)

获取一个引用,并在其开头附加 javascript:,可以在客户端事件中使用该引用,并将该引用与指定的事件参数一起使用,以便回发到指定控件的服务器。

GetPostBackClientHyperlink(Control, String, Boolean)

获取一个引用,并在其开头附加 javascript:,该引用可用于在客户端事件中回发到指定控件的服务器,回发时使用指定的事件参数和一个指示是否为事件验证注册该回发的布尔值。

GetPostBackClientHyperlink(Control, String)

获取一个引用,并在其开头附加 javascript:,可以在客户端事件中使用该引用,并将该引用与指定的事件参数一起使用,以便回发到指定控件的服务器。

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

参数

control
Control

处理回发的服务器控件。

argument
String

传递给服务器控件的参数。

返回

String

一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用。

示例

下面的代码示例演示了该方法的使用 GetPostBackClientHyperlink 。 自定义控件 MyControl实现 IPostBackEventHandler 接口。 单击页面上的 HTML 定位点元素时, 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>

下面的代码示例的功能与前面的代码相同,只不过该类实现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>
<%@ 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>

注解

GetPostBackEventReference使用该方法需要处理回发以实现接口的IPostBackEventHandler控件。 若要实现 IPostBackEventHandler 接口 Page,请使用 @ Implements 指令。

另请参阅

适用于

GetPostBackClientHyperlink(Control, String, Boolean)

获取一个引用,并在其开头附加 javascript:,该引用可用于在客户端事件中回发到指定控件的服务器,回发时使用指定的事件参数和一个指示是否为事件验证注册该回发的布尔值。

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

参数

control
Control

处理回发的服务器控件。

argument
String

传递给服务器控件的参数。

registerForEventValidation
Boolean

true 表示为验证注册回发事件;false 表示不为验证注册回发事件。

返回

String

一个字符串,表示对包含目标控件的 ID 和事件参数的回发函数的 JavaScript 调用。

注解

GetPostBackEventReference使用该方法需要处理回发以实现接口的IPostBackEventHandler控件。 若要实现 IPostBackEventHandler 接口 Page,请使用 @ Implements 指令。

另请参阅

适用于