ASP.NET - 이벤트
- 이전 페이지 WebForms 컨트롤
- 다음 페이지 WebForms 폼
이벤트 핸들러(event handler)는 주어진 이벤트에 대해 코드를 실행하는 서브루틴입니다.
ASP.NET - 이벤트 핸들러
아래 코드를 보세요:
<% lbl1.Text="The date and time is " & now() %> <html> <body> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3> </form> </body> </html>
위 코드가 언제 실행될까요? 답은 ‘나는 모르겠어요…’입니다.
Page_Load 이벤트
Page_Load 이벤트는 ASP.NET에서 이해할 수 있는 많은 이벤트 중 하나입니다. Page_Load 이벤트는 페이지가 로드될 때 발생하며, 그 다음 ASP.NET는 자동으로 Page_Load 서브루틴을 호출하고 그 안의 코드를 실행합니다:
<script runat="server"> Sub Page_Load lbl1.Text="The date and time is " & now() End Sub </script> <html> <body> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3> </form> </body> </html>
주석:이 Page_Load 이벤트는 객체 참조나 이벤트 파라미터를 포함하지 않습니다!
Page.IsPostBack 속성
Page_Load 서브루틴은 페이지가 매번 로드될 때마다 실행됩니다. 페이지가 최초로 로드될 때에만 Page_Load 서브루틴 내의 코드를 실행하려면 Page.IsPostBack 속성을 사용할 수 있습니다. Page.IsPostBack 속성이 false면 페이지가 처음으로 로드되었음을 의미하며, true면 페이지가 서버로 반환되었음을 의미합니다(예를 들어, 양식의 버튼을 클릭하면):
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then lbl1.Text="The date and time is " & now() end if End Sub Sub Submit(s As Object, e As EventArgs) lbl2.Text="Hello World!" End Sub </script> <html> <body> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3> <h3><asp:label id="lbl2" runat="server" /></h3> <asp:button text="Submit" onclick="submit" runat="server" /> </form> </body> </html>
위의 예제는 페이지가 최초로 로드될 때 "The date and time is...." 이 메시지를 생성합니다. 사용자가 Submit 버튼을 클릭하면 submit 서브루틴이 두 번째 레이블에서 "Hello World!"을 생성하지만, 첫 번째 레이블의 날짜와 시간은 변경되지 않습니다.
- 이전 페이지 WebForms 컨트롤
- 다음 페이지 WebForms 폼