Latest web development tutorials

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>

위의 코드는 때 실행됩니다? 대답은 "나도 몰라 ..."


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 속성을 사용할 수 있습니다 때. . true로 설정하면 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>

데모 >>

위의 예는 "날짜와 시간은 ....입니다"표시 페이지가 처음로드 메시지에만 사용할 수 있습니다. 사용자가 제출 버튼입니다 클릭하면 서브 루틴이 두 번째 레이블에 "Hello World!"를 작성합니다 제출하지만, 첫 번째 레이블의 날짜와 시간은 변경되지 않습니다.