HtmlButton.OnServerClick(EventArgs) 메서드

정의

ServerClick 이벤트를 발생시킵니다. 이를 통해 이벤트에 대한 사용자 지정 처리기를 제공할 수 있습니다.

protected:
 virtual void OnServerClick(EventArgs ^ e);
protected virtual void OnServerClick (EventArgs e);
abstract member OnServerClick : EventArgs -> unit
override this.OnServerClick : EventArgs -> unit
Protected Overridable Sub OnServerClick (e As EventArgs)

매개 변수

e
EventArgs

이벤트 데이터를 포함하는 EventArgs입니다.

예제

다음 코드 예제에서는 선언적으로 지정 하 고 이벤트에 대 한 이벤트 처리기를 코딩 하는 방법을 보여 줍니다 ServerClick . 컨트롤을 HtmlButton 클릭하면 텍스트 상자에 입력된 값이 표시됩니다.

<%@ Page Language="C#" AutoEventWireup="True" %>

<!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" >
    <script language="C#" runat="server">

      protected void FancyBtn_Click(object sender, EventArgs e)
      {  
        Message.InnerHtml = "Your name is: " + Name.Value; 
      }
      
</script>
  
    <head runat="server">
    <title> Enter Name: </title>
</head>
<body>
          <form id="form1" method="post" runat="server">
  
            <h3> Enter Name: <input id="Name" type="text" size="40" runat="server" />
            </h3>
  
             <button onserverclick=" FancyBtn_Click" runat="server" id="BUTTON1">
               <b><i> I'm a fancy HTML 4.0 button </i> </b> 
             </button>
  
                       
           <h1>
             <span id="Message" runat="server"></span>
           </h1>
  
          </form>
       </body>
 </html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!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" >
    <script language="VB" runat="server">
       Sub FancyBtn_Click(Source As Object, E as EventArgs)
          Message.InnerHtml = "Your name is: " & Name.Value
       End Sub
    </script>
  
    <head runat="server">
    <title> Enter Name: </title>
</head>
<body>
          <form id="form1" method="post" runat="server">
  
            <h3> Enter Name: <input id="Name" type="text" size="40" runat="server" />
            </h3>
  
             <button onserverclick=" FancyBtn_Click" runat="server">
               <b><i> I'm a fancy HTML 4.0 button </i> </b> 
             </button>
  
                       
           <h1>
             <span id="Message" runat="server"></span>
           </h1>
  
          </form>
       </body>
 </html>

다음 코드 예제에서는 프로그래밍 방식으로 지정 하 고 이벤트에 대 한 이벤트 처리기를 코딩 하는 방법을 보여 줍니다 ServerClick .


<%@ Page Language="C#" AutoEventWireup="True" %>

<!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>
    <title> HtmlButton Constructor Example </title>
<script runat="server">

      void Page_Load(Object sender, EventArgs e)
      {

         // Create a new HtmlButton control.
         HtmlButton NewButtonControl = new HtmlButton();

         // Set the properties of the new HtmlButton control.
         NewButtonControl.ID = "NewButtonControl";
         NewButtonControl.InnerHtml = "Click Me";

         // Create an EventHandler delegate for the method you want to handle the event
         // and then add it to the list of methods called when the event is raised.
         NewButtonControl.ServerClick += new System.EventHandler(this.Button_Click); 

         // Add the new HtmlButton control to the Controls collection of the
         // PlaceHolder control. 
         ControlContainer.Controls.Add(NewButtonControl);

      }

      void Button_Click(Object sender, EventArgs e)
      {

         // Display a simple message. 
         Message.InnerHtml = "Thank you for clicking the button.";

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlButton Constructor Example </h3>

      <asp:PlaceHolder ID="ControlContainer"
           runat="server"/>

      <br /><br />
 
      <span id="Message"
            runat="server"/>

   </form>

</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>

<!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>
    <title> HtmlButton Constructor Example </title>
<script runat="server">

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Create a new HtmlButton control.
         Dim NewButtonControl As New HtmlButton()

         ' Set the properties of the new HtmlButton control.
         NewButtonControl.ID = "NewButtonControl"
         NewButtonControl.InnerHtml = "Click Me"

         ' Create an EventHandler delegate for the method you want to handle the event
         ' and then add it to the list of methods called when the event is raised.
         AddHandler NewButtonControl.ServerClick, AddressOf Button_Click 

         ' Add the new HtmlButton control to the Controls collection of the
         ' PlaceHolder control. 
         ControlContainer.Controls.Add(NewButtonControl)

      End Sub

      Sub Button_Click(sender As Object, e As EventArgs)

         ' Display a simple message. 
         Message.InnerHtml = "Thank you for clicking the button."

      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlButton Constructor Example </h3>

      <asp:PlaceHolder ID="ControlContainer"
           runat="server"/>

      <br /><br />
 
      <span id="Message"
            runat="server"/>

   </form>

</body>
</html>

설명

ServerClick 컨트롤을 클릭할 때 HtmlButton 이벤트가 발생합니다. 이 이벤트로 인해 왕복이 클라이언트에서 서버로 다시 발생합니다. 클라이언트 쪽 OnClick 이벤트와 의도적으로 다릅니다. 이벤트가 있는 코드 실행 ServerClick 과 클라이언트 쪽 이벤트에서 실행하는 코드 간에 충돌이 있는 경우 서버 쪽 OnClick 이벤트 지침은 클라이언트 쪽 코드를 재정의합니다.

이벤트가 발생하면 대리자를 통해 이벤트 처리기가 호출됩니다. 자세한 내용은 이벤트 처리 및 발생합니다.

또한 OnServerClick 메서드를 사용하면 파생 클래스가 대리자를 연결하지 않고도 이벤트를 처리할 수 있습니다. 이는 파생 클래스에서 이벤트를 처리하는 기본 방법입니다.

상속자 참고

파생 클래스에서 OnServerClick(EventArgs)를 재정의하는 경우 등록된 대리자가 이벤트를 받도록 기본 클래스의 OnServerClick(EventArgs) 메서드를 호출해야 합니다.

적용 대상

추가 정보