|
|
ASP.NET |
The Page_Load subroutine runs EVERY time the page is loaded.
If you want to execute the code in the Page_Load subroutine only the FIRST time the page is loaded, you can use the Page.IsPostBack property.
If the Page.IsPostBack property is false, the page is loaded for the first time.
If Page.IsPostBack property is true, the page is posted back to the server (i.e. from a button click on a form):
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then lbl1.Text="The page is loaded first time & date and time is " & now() end if End Sub Sub submit(s As Object, e As EventArgs)
lbl2.Text="Hello user!, Page is back from server... i.e not the first time."
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 example above will write the "This page is ...." message only the first time the page is loaded.
When a user clicks on the Submit button, the submit subroutine will write "Hello user ......" to the second label, but the date and time in the first label will not change.