최신 웹 개발 튜토리얼
 

ASP.NET웹 양식 - 이벤트


이벤트 핸들러는 특정 이벤트에 대한 코드를 실행하는 서브 루틴이다.


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>

때 위의 코드는 실행됩니다? 대답은 : "You don't know..."


를 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>
»예보기

Note: Page_Load 이벤트는 객체 참조 또는 이벤트 인수에는 포함되어 있지 않습니다!


Page.IsPostBack 속성

를 Page_Load 서브 루틴은 페이지가로드 될 때마다 실행됩니다. 당신이를 Page_Load 서브 루틴에서 페이지가로드 처음에만 코드를 실행하려는 경우 Page.IsPostBack 속성을 사용할 수 있습니다. Page.IsPostBack 속성이 false 인 경우, 페이지가 그것이 사실 인 경우, 페이지가 서버에 다시 게시 처음로드 (즉, 폼에 버튼을 클릭 기준) :

<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...." 메시지 페이지가로드되는 첫 번째 시간입니다. 사용자가 버튼을 제출을 클릭하면 서브 루틴을 쓸 것이다 제출 "Hello World!" 두 번째 레이블하지만, 첫 번째 레이블의 날짜와 시간은 변경되지 않습니다.