웹 페이지 튜토리얼로그인
웹 페이지 튜토리얼
작가:php.cn  업데이트 시간:2022-04-11 14:20:28

웹 양식 이벤트


ASP.NET Web Forms - 이벤트


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


ASP.NET - 이벤트 핸들러

아래 코드를 참조하세요:

<%
lbl1.Text="날짜와 시간은 " & 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 서브루틴을 호출하고 그 안의 코드를 실행합니다.

Instance

<script  runat="server">
Sub Page_Load
   lbl1.Text="The date and time is " & now()
End Sub
</script>

<!DOCTYPE html>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>

Run Instance»

"인스턴스 실행" 버튼을 클릭하세요. 온라인으로 보기 예

참고: Page_Load 이벤트에는 개체 참조나 이벤트 매개변수가 포함되어 있지 않습니다!


Page.IsPostBack 속성

Page_Load 서브루틴은 페이지가 로드될 때마다 실행됩니다. 페이지가 처음 로드될 때만 Page_Load 서브루틴의 코드를 실행하려면 Page.IsPostBack 속성을 사용하면 됩니다. Page.IsPostBack 속성이 false로 설정된 경우 페이지가 처음으로 로드되고, true로 설정된 경우 페이지가 서버에 다시 게시됩니다(예: 양식의 버튼 클릭).

Example

<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>

<!DOCTYPE html>
<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!"를 쓰지만 첫 번째 레이블의 날짜와 시간은 변경되지 않습니다.


PHP 중국어 웹사이트