방법: 이벤트를 발생시킨 웹 서버 컨트롤 확인

업데이트: 2007년 11월

이벤트 처리기가 호출되면 이벤트를 발생시킨 컨트롤을 확인할 수 있습니다.

이벤트를 발생시킨 컨트롤을 확인하려면

  1. 이벤트 처리기에서 이벤트를 발생시킨 컨트롤과 형식이 일치하는 변수를 선언합니다.

  2. 이벤트 처리기의 sender 인수를 변수에 할당하고 적절한 형식으로 캐스팅합니다.

    다음 예제에서는 여러 개의 다른 단추에 의해 호출되는 Button 컨트롤 클릭 이벤트에 대한 처리기를 보여 줍니다. 이 처리기는 클릭한 단추에 대한 정보를 표시합니다.

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click, Button2.Click, Button3.Click
    
        Dim b As Button
        b = CType(sender, Button)
    
        Select Case b.ID
            Case "Button1"
                Label1.Text = "You clicked the first button"
            Case "Button2"
                Label1.Text = "You clicked the second button"
            Case "Button3"
                Label1.Text = "You clicked the third button"
        End Select
    End Sub
    
    private void Button_Click(object sender, System.EventArgs e)
    {
        Button b;
        b = (Button)sender;
        switch (b.ID)
        {
            case "Button1":
                Label1.Text = "You clicked the first button";
                break;
            case "Button2":
                Label1.Text = "You clicked the second button";
                break;
            case "Button3":
                Label1.Text = "You clicked the third button";
                break;
        }
    }
    

참고 항목

개념

ASP.NET 웹 서버 컨트롤 이벤트 모델

기타 리소스

ASP.NET 웹 페이지에서 서버 이벤트 처리

이벤트 처리 및 발생