ClientScriptManager.ValidateEvent 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
驗證客戶事件。
多載
| 名稱 | Description |
|---|---|
| ValidateEvent(String) |
驗證已註冊用於事件驗證的客戶端事件,該事件使用該 RegisterForEventValidation(String) 方法。 |
| ValidateEvent(String, String) |
驗證已註冊用於事件驗證的客戶端事件,該事件使用該 RegisterForEventValidation(String, String) 方法。 |
ValidateEvent(String)
驗證已註冊用於事件驗證的客戶端事件,該事件使用該 RegisterForEventValidation(String) 方法。
public:
void ValidateEvent(System::String ^ uniqueId);
public void ValidateEvent(string uniqueId);
member this.ValidateEvent : string -> unit
Public Sub ValidateEvent (uniqueId As String)
參數
- uniqueId
- String
一個代表產生事件的用戶端控制的獨特 ID。
另請參閱
適用於
ValidateEvent(String, String)
驗證已註冊用於事件驗證的客戶端事件,該事件使用該 RegisterForEventValidation(String, String) 方法。
public:
void ValidateEvent(System::String ^ uniqueId, System::String ^ argument);
public void ValidateEvent(string uniqueId, string argument);
member this.ValidateEvent : string * string -> unit
Public Sub ValidateEvent (uniqueId As String, argument As String)
參數
- uniqueId
- String
一個代表產生事件的用戶端控制的獨特 ID。
- argument
- String
事件參數會隨著客戶端事件一起通過。
例外狀況
uniqueId 是 null 空字串(“”)。
範例
以下程式碼範例示範使用該 RegisterForEventValidation 方法與方法 ValidateEvent 來註冊回調以供驗證,並驗證回調是否來自頁面。 為了改進此處所示的驗證,你可以修改驗證 argument 參數,包含使用者特定的資訊,例如身份或角色
<%@ Page Language="C#" %>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" >
string _cbMessage = "";
// Define method that processes the callbacks on server.
public void RaiseCallbackEvent(String eventArgument)
{
try
{
Page.ClientScript.ValidateEvent(button1.UniqueID, this.ToString());
_cbMessage = "Correct event raised callback.";
}
catch (Exception ex)
{
_cbMessage = "Incorrect event raised callback.";
}
}
// Define method that returns callback result.
public string GetCallbackResult()
{
return _cbMessage;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ClientScriptManager cs = Page.ClientScript;
String cbReference = cs.GetCallbackEventReference("'" +
Page.UniqueID + "'", "arg", "ReceiveServerData", "",
"ProcessCallBackError", false);
String callbackScript = "function CallTheServer(arg, context) {" +
cbReference + "; }";
cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer",
callbackScript, true);
}
}
protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(button1.UniqueID, this.ToString());
base.Render(writer);
}
</script>
<script type="text/javascript">
var value1 = new Date();
function ReceiveServerData(arg, context)
{
Message.innerText = arg;
Label1.innerText = "Callback completed at " + value1;
value1 = new Date();
}
function ProcessCallBackError(arg, context)
{
Message.innerText = 'An error has occurred.';
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CallBack Event Validation Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
Callback result: <span id="Message"></span>
<br /> <br />
<input type="button"
id="button1"
runat="server"
value="ClientCallBack"
onclick="CallTheServer(value1, null )"/>
<br /> <br />
<asp:Label id="Label1" runat="server"/>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Dim _cbMessage As String = ""
' Define method that processes the callbacks on server.
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
Try
Page.ClientScript.ValidateEvent(button1.UniqueID, Me.ToString())
_cbMessage = "Correct event raised callback."
Catch ex As Exception
_cbMessage = "Incorrect event raised callback."
End Try
End Sub
' Define method that returns callback result.
Public Function GetCallbackResult() _
As String Implements _
System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return _cbMessage
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If (Not IsPostBack) Then
Dim cs As ClientScriptManager = Page.ClientScript
Dim cbReference As String = cs.GetCallbackEventReference("'" & _
Page.UniqueID & "'", "arg", "ReceiveServerData", "", _
"ProcessCallBackError", False)
Dim callbackScript As String = "function CallTheServer(arg, context) {" & _
cbReference & "; }"
cs.RegisterClientScriptBlock(Me.GetType(), "CallTheServer", _
callbackScript, True)
End If
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Page.ClientScript.RegisterForEventValidation(button1.UniqueID, Me.ToString())
MyBase.Render(writer)
End Sub
</script>
<script type="text/javascript">
var value1 = new Date();
function ReceiveServerData(arg, context)
{
Message.innerText = arg;
Label1.innerText = "Callback completed at " + value1;
value1 = new Date();
}
function ProcessCallBackError(arg, context)
{
Message.innerText = 'An error has occurred.';
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CallBack Event Validation Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
Callback result: <span id="Message"></span>
<br /> <br />
<input type="button"
id="button1"
runat="server"
value="ClientCallBack"
onclick="CallTheServer(value1, null )"/>
<br /> <br />
<asp:Label id="Label1" runat="server"/>
</div>
</form>
</body>
</html>