ClientScriptManager.GetPostBackClientHyperlink 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會取得一個引用,並且 javascript: 在引用前加上,可以在客戶端事件中使用,並回傳給伺服器。
多載
| 名稱 | Description |
|---|---|
| GetPostBackClientHyperlink(Control, String) |
會取得一個引用,並在 |
| GetPostBackClientHyperlink(Control, String, Boolean) |
會取得一個引用,並 |
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
參數會傳給伺服器控制。
傳回
一個代表 JavaScript 呼叫 postback 函式的字串,包含目標控制項的 ID 與事件參數。
範例
以下程式碼範例示範了此 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>
以下程式碼範例功能與前一個相同,不同之處在於該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>
備註
使用此 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 不要註冊回傳事件來驗證。
傳回
一個代表 JavaScript 呼叫 postback 函式的字串,包含目標控制項的 ID 與事件參數。
備註
使用此 GetPostBackEventReference 方法需要處理回貼的控制項來實作介面 IPostBackEventHandler 。 要實作 IPostBackEventHandler 的 Page介面,請使用指令 @ Implements 。