order of execution in asp.net page?

Results 1 to 3 of 3

Thread: order of execution in asp.net page?

  1. #1
    Join Date
    Dec 1969

    Default order of execution in asp.net page?

    In a book I&#039;m reading, the author says that the Init executes before the Page_Load event. Then in another exercise they show some code where it looks like the InitializeComponent method is calling a delegate that calls the function for that delegate and then executes the Page_Load method. So, I wanted to make sure that&#039;s what was going on, so I modified my code a bit and ran a test. If what I&#039;m thinking is correct, then after clicking on button1, I would get the following message...<BR><BR>You clicked button 1<BR>Page Load Occurred.<BR><BR><BR>However, I got exactly the opposite which tells me that the Page_Load occurred first, and then the delegate called the method it pointed to. I&#039;m new to asp.net, so I&#039;m sure there&#039;s something I don&#039;t understand here, but basically, I want to make sure I&#039;m clear on how an asp.net page executes so I know where to put what code. Here&#039;s the pages I have.<BR><BR>button.aspx<BR>&#060;%@Page language="C#" Inherits="Chapter3.Button" AutoEventWireup="false"%&#062;<BR>&#060;html&#062; &#060;head&#062;&#060;/head&#062;<BR>&#060;body&#062;<BR>&#060;form method="post" runat="server"&#062;<BR>&#060;asp:Label id="Label1" runat="server" text="Nothing Clicked"/&#062;<BR>&#060;asp:Button id=Button1 runat="server" text="Button 1" commandname="Button1" commandargument="Arg1"/&#062;<BR>&#060;asp:Button id=Button2 runat="server" text="Button 2" commandname="Button2" commandargument="Arg2"/&#062;<BR>&#060;asp:Button id=Button3 runat="server" text="Button 3" commandname="Button3" commandargument="Arg3"/&#062;<BR>&#060;/form&#062;&#060;/body&#062;<BR>&#060;/html&#062;<BR><BR>button.cs...<BR>using System;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Data;<BR>using System.Drawing;<BR>using System.Web;<BR>using System.Web.SessionState;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.HtmlControls;<BR><BR>namespace Chapter3<BR>{<BR> public class Button : System.Web.UI.Page<BR> {<BR> protected System.Web.UI.WebControls.Button Button2;<BR> protected System.Web.UI.WebControls.Label Label1;<BR> protected System.Web.UI.WebControls.Button Button1;<BR> protected System.Web.UI.WebControls.Button Button3;<BR> protected string msg = "";<BR> <BR> public Button()<BR> {<BR> Page.Init += new System.EventHandler(Page_Init);<BR> }<BR> <BR> protected void Page_Load(object sender, System.EventArgs e)<BR> {<BR> msg += "Page Load Occurred.";<BR> }<BR> <BR> protected void Page_Init(object sender, EventArgs e)<BR> {<BR> InitializeComponent();<BR> }<BR> <BR> #region Web Form Designer generated code<BR> private void InitializeComponent()<BR> {<BR> this.Button1.Command += new CommandEventHandler(this.Button_Command);<BR> this.Button2.Command += new CommandEventHandler(this.Button_Command);<BR> this.Button3.Command += new CommandEventHandler(this.Button_Command);<BR> this.Load += new System.EventHandler(this.Page_Load);<BR> }<BR> #endregion<BR> <BR> private void Button_Command(object sender, CommandEventArgs e)<BR> {<BR> switch (e.CommandArgument.ToString())<BR> {<BR> case "Arg1":<BR> msg += "You clicked button 1";<BR> break;<BR> case "Arg2":<BR> msg += "You clicked button 2";<BR> break;<BR> default:<BR> msg += "You clicked button 3";<BR> break;<BR> }<BR> Label1.Text = msg;<BR> }<BR> }<BR>}

  2. #2
    Join Date
    Dec 1969

    Default RE: order of execution in asp.net page?

    Page_Init is run before page_load ONLY on the first time a page is accessed. If IsPostBack is True then page_init is not run. Hope that helps.

  3. #3
    Join Date
    Dec 1969

    Default Incorrect. Init always runs.

    Page_Init always executes. This is why in many cases in your page_load event handler you want to put in a check for !IsPostBack. After Page_Init is complete on postbacks it loads the view state and then calls event handlers.<BR><BR>Easiest way to look at what is happening is to turn page tracing on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts