다음을 통해 공유


ClientScriptManager.RegisterForEventValidation 메서드

정의

유효성 검사에 대한 이벤트 참조를 등록합니다.

오버로드

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 id 또는 역할과 같은 사용자 관련 정보를 포함 하도록 매개 변수

<%@ 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

클라이언트 JavaScript를 생성하여 다시 게시 이벤트를 시작하는 방법을 지정하는 PostBackOptions 개체입니다.

설명

자세한 내용 및 예제에 대 한 참조를 RegisterForEventValidation 메서드.

추가 정보

적용 대상