ASP.NET Tutorial/Page Lifecycle/Post Back

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Page.IsPostBack (C#)

<%@ Page Language="C#" %>
<script runat="server">
   void Page_Load(Object Sender, EventArgs e) {
      if (!Page.IsPostBack) {
         lblMessage.Text = "Hello world!!!!";
      }
   }
   
   void Submit(Object Sender, EventArgs e) {
      lblMessage2.Text = "Form posted";
   }
</script>
<html><body>
   <form runat="server">
      <asp:Button id="btSubmit" runat="server" Text="Submit" onClick="Submit" />
      
      <asp:Label id="lblMessage" runat=server />
      
      <asp:Label id="lblMessage2" runat=server />
   </form>
</body></html>


Save value to view state (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
   Sub Page_Load(Sender as Object, e as EventArgs)
      if not Page.IsPostBack then
         ViewState("StartTime") = DateTime.Now      
         lblMessage.Text = "The time is now: " & _
            ViewState("StartTime")
      end if
   end sub
   
   Sub Submit(obj as object, e as EventArgs)
      lblMessage.Text = "The time is now: " & DateTime.Now & _
         "<br>started at: " & ViewState("StartTime")
      
   end Sub
</script>
<html><body>
   <form runat="server">
      <asp:Button id="btSubmit" runat="server" Text="Submit"
         onClick="Submit" />
      
      <asp:Label id="lblMessage" runat=server />
   </form>
</body></html>