Web Forms 이벤트 처리는 웹 페이지에서 사용자가 클릭하는 컨트롤에 따라 이벤트 작업을 수행한다.
이벤트는 개체에서 작업 실행을 알리기 위해 보내는 메시지이다.
이 작업은 단추 클릭과 같은 사용자 조작으로 발생하거나, 속성 값 변경 등의 다른 프로그램 논리에서 발생할 수 있다.
이벤트를 발생시키는 개체를 이벤트 전송자라고 하며 이벤트 전송자는 어떤 개체 또는 메서드가 발생되는 이벤트를 수신(처리)할지는 모른다.
이벤트 순서
1. 웹 페이지에서 OnClick 컨트롤이 있는 Button
다음 단계에서 정의할 메서드의 이름으로 설정된 Button 값을 가진 OnClick 컨트롤이 있는 ASP.NET Web Forms 페이지(웹 페이지)를 만든다.
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
2. Click 이벤트를 처리
OnClick 값에 대해 정의한 이름을 갖는 이벤트 처리기를 정의한다.
protected void Button1_Click(object sender, EventArgs e)
{
// perform action
}
이벤트 매개변수
이벤트 핸들러에는 두 개의 매개변수를 전달하는데 object sender와 EventArgs e이다.
object sender는 이벤트를 발생시키는 개체이고 EventArgs는 이벤트에 대한 정보를 가지고 있는 개체라고 할 수 있다.
EventArgs는 이벤트 데이터를 포함하는 클래스의 기본 클래스를 나타내며 이벤트 데이터를 포함하지 않는 이벤트에 사용할 값을 제공한다.
<%@ 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>
'프로그래밍 언어 > C#' 카테고리의 다른 글
구글 api 사용하여 SNS 로그인 연동 (0) | 2023.09.20 |
---|---|
[ASP.NET][C#] Response 개체 (0) | 2023.02.17 |
[ASP.NET][C#] Request 개체 (0) | 2023.02.17 |
[ASP.NET][C#] postback 포스트백 (0) | 2023.02.16 |
[C#] 기본 구조 (0) | 2022.11.06 |
댓글