ClientScriptManager.RegisterForEventValidation 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
註冊事件參考以進行驗證。
多載
RegisterForEventValidation(String, String) |
使用唯一的控制項 ID 和事件引數 (表示產生事件的用戶端控制項) 註冊事件參考以進行驗證。 |
RegisterForEventValidation(String) |
使用唯一的控制項 ID (表示產生事件的用戶端控制項) 註冊事件參考以進行驗證。 |
RegisterForEventValidation(PostBackOptions) |
使用 PostBackOptions 註冊事件參考以進行驗證。 |
RegisterForEventValidation(String, String)
使用唯一的控制項 ID 和事件引數 (表示產生事件的用戶端控制項) 註冊事件參考以進行驗證。
public:
void RegisterForEventValidation(System::String ^ uniqueId, System::String ^ argument);
public void RegisterForEventValidation (string uniqueId, string argument);
member this.RegisterForEventValidation : string * string -> unit
Public Sub RegisterForEventValidation (uniqueId As String, argument As String)
參數
- uniqueId
- String
唯一的 ID,表示產生事件的用戶端控制項。
- argument
- String
隨用戶端事件傳遞的事件引數。
例外狀況
這個方法會在 Render(HtmlTextWriter) 方法之前呼叫。
範例
下列程式碼範例示範如何使用 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>
下列程式碼範例示範如何使用 RegisterForEventValidation 方法來註冊回呼以進行驗證。
<%@ Page EnableEventValidation="true" Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Register an option for event validation</title>
<script runat="server">
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 1");
ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 2");
ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 3");
// Uncomment the line below when you want to specifically register the option for event validation.
// ClientScript.RegisterForEventValidation("DropDownList1", "Is this option registered for event validation?");
base.Render(writer);
}
</script>
<script type="text/javascript">
function Initialize()
{
var oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "This is Option 1";
oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "This is Option 2";
oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "This is Option 3";
oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "Is this option registered for event validation?";
}
</script>
</head>
<body onload="Initialize();">
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Postback to server for validation" />
</div>
</form>
</body>
</html>
<%@ Page EnableEventValidation="true" Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Register an option for event validation</title>
<script runat="server">
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 1")
ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 2")
ClientScript.RegisterForEventValidation("DropDownList1", "This is Option 3")
' Uncomment the line below when you want to specifically register the option for event validation.
' ClientScript.RegisterForEventValidation("DropDownList1", "Is this option registered for event validation?")
MyBase.Render(writer)
End Sub
</script>
<script type="text/javascript">
function Initialize()
{
var oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "This is Option 1";
oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "This is Option 2";
oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "This is Option 3";
oOption = document.createElement("OPTION");
document.all("DropDownList1").options.add(oOption);
oOption.innerText = "Is this option registered for event validation?";
}
</script>
</head>
<body onload="Initialize();">
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Postback to server for validation" />
</div>
</form>
</body>
</html>
另請參閱
適用於
RegisterForEventValidation(String)
使用唯一的控制項 ID (表示產生事件的用戶端控制項) 註冊事件參考以進行驗證。
public:
void RegisterForEventValidation(System::String ^ uniqueId);
public void RegisterForEventValidation (string uniqueId);
member this.RegisterForEventValidation : string -> unit
Public Sub RegisterForEventValidation (uniqueId As String)
參數
- uniqueId
- String
唯一的 ID,表示產生事件的用戶端控制項。
範例
下列範例示範如何使用 RegisterForEventValidation 方法和 ValidateEvent 方法來註冊回呼以進行驗證,以及如何驗證來自頁面的回呼。
<%@ 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);
_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);
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 id="Head1" 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)
_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)
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>
備註
如需詳細資訊和範例,請參閱 RegisterForEventValidation 方法。
另請參閱
適用於
RegisterForEventValidation(PostBackOptions)
使用 PostBackOptions 註冊事件參考以進行驗證。
public:
void RegisterForEventValidation(System::Web::UI::PostBackOptions ^ options);
public void RegisterForEventValidation (System.Web.UI.PostBackOptions options);
member this.RegisterForEventValidation : System.Web.UI.PostBackOptions -> unit
Public Sub RegisterForEventValidation (options As PostBackOptions)
參數
- options
- PostBackOptions
PostBackOptions 物件,指定產生用戶端 JavaScript 以啟始回傳事件的方式。
備註
如需詳細資訊和範例,請參閱 RegisterForEventValidation 方法。