Button.OnClick(EventArgs) 메서드

정의

Click 컨트롤의 Button 이벤트를 발생시킵니다.

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

매개 변수

e
EventArgs

이벤트 데이터입니다.

예제

다음 코드 예제를 지정 하 고 웹 페이지에 간단한 메시지를 표시 하기 위해 이벤트에 대 한 Click 이벤트 처리기를 코딩 하는 방법을 보여 줍니다.

<%@ 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">
    
    void Page_Load(Object sender, EventArgs e)
    {
        // Manually register the event-handling method for
        // the Click event of the Button control.
        Button1.Click += new EventHandler(this.GreetingBtn_Click);
    }

    void GreetingBtn_Click(Object sender,
                           EventArgs e)
    {
        // When the button is clicked,
        // change the button text, and disable it.

        Button clickedButton = (Button)sender;
        clickedButton.Text = "...button clicked...";
        clickedButton.Enabled = false;
        
        // Display the greeting label text.
        GreetingLabel.Visible = true;
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <h3>Simple Button Example</h3>
     
      <asp:Button id="Button1"
           Text="Click here for greeting..."
           OnClick="GreetingBtn_Click" 
           runat="server"/>
      <br />
      <br />
      <asp:Label ID="GreetingLabel" runat="server" 
                 Visible="false" Text="Hello World!" />
    </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">
    
    Sub GreetingBtn_Click(ByVal sender As Object, _
                          ByVal e As EventArgs)
   
        ' When the button is clicked,
        ' change the button text, and disable it.
        
        Dim clickedButton As Button = sender
        clickedButton.Text = "...button clicked..."
        clickedButton.Enabled = False
 
        ' Display the greeting label text.
        GreetingLabel.Visible = True
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Button Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <h3>Simple Button Example</h3>
     
      <asp:Button id="Button1"
           Text="Click here for greeting..."
           OnClick="GreetingBtn_Click" 
           runat="server"/>
      <br />
      <br />
      <asp:Label ID="GreetingLabel" runat="server" 
                 Visible="false" Text="Hello World!" />
    </div>
    </form>
</body>
</html>

설명

Click 컨트롤을 클릭할 때 Button 이벤트가 발생합니다. 이 이벤트는 컨트롤과 Button 연결된 명령 이름이 없는 경우에 일반적으로 사용됩니다(instance 경우 제출 단추 사용).

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

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

상속자 참고

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

적용 대상

추가 정보