How to Implement Page Life Cycle Concepts in Asp.Net with Example

By
Hi Friends ! Today I am going to Explain Page Life Concepts in Asp.Net with real Life Examples.Some My Dear Students Demand me this post with Real Life examples.so today , i am going to publish this post with some examples.
           When an asp.net Page runs,the page perform a series of steps such as Initialization , Instantiating controls, Maintaining & Restoring state,Event Handler,Rendering, that is called page life.  
Rendering :-It is a process of creating a visual representation on a Display Surface.
There are some various steps of page life cycle in asp.net which are given below:-
  • PreInit
  • Init
  • InitComplete
  • PreLoad
  • Load
  • Control Events
  • LoadComplete
  • PreRender
  • SaveStateComplete
  • Render
  • Unload
Note:- Page life cycle is the sequence of  steps which are followed by the all visual studio Compilers. 
Real Life Example 1:- Software Industries
Before Understanding the concepts of Page Life cycle ,First we have to understand the concepts of Software life cycle in Software Engineering.
Hi Friends ! You may be aware  of software life cycle, Means what are the steps involved to build a Software. You can't break any steps to develop a  good software.Now days all the software Industries follow these steps for developing  and software (big,small)  very carefully. If you escape any steps then software may not be bug/error. Similarly asp.net compiler follow each steps (Events) when any page display on the browser. There are some steps of Software life cycle as given below:-
  1. Planning
  2. Designing
  3. Coding (Implementation)
  4. Testing
  5. Documentation
  6. Deployment
  7. Maintenance
Now i am going to explain page life cycle in asp.net with each steps,There are following events occurs to load a complete page in asp.net as given below:-
    1.) PreInit :- PreInit is the first stage of asp.net page event life cycle.In this stage following events can be fired on asp.net page.
  • Check the IsPostBack Property to determine whether This page is processing first time.
  • Create & Recreate Dynamic controls
  • Set A Master page in the code dynamically
  • Set the theme & Skin property on the page 
Note:-
  • If the request is a Post back ,the value of controls have not been stored from view state.  
  • View State :- View state stores page-specific information,when a page post back to the server. Read More Details here...
Example of PreInit :- 

protected void Page_PreInit(object sender, EventArgs e)
    {
        // Text Box Control Dynamically
        TextBox txt = new TextBox();
        txt.ID = "mytext";
        txt.EnableTheming = true;
        txt.Text = "I am TextBox";
    //When you create below event handler press Tab 2 times from your keyboard
        txt.TextChanged += new EventHandler(txt_TextChanged);
        this.form1.Controls.Add(txt);
        txt.Text = "Change Text&press Submit";

        //Page.MasterPageFile = "~/MasterPage.master";

        //Checking whether value is strored From view state or not in preIntit Event
        int i = Convert.ToInt32(ViewState["p"]) + 1;
        ViewState["p"] = i.ToString();
        Label1.Text = " Hi..";
  Label1.Text = Label1.Text + "<br/>" + " I am PreInit Event " + ViewState["p"];

        // create button control dynamically
        Button submit_button = new Button();
        submit_button.ID ="btntxt";
        submit_button.Text="Submit";
    //When you create below event handler press Tab 2 times from your keyboard
        submit_button.Click +=new EventHandler(submit_button_Click);
        this.form1.Controls.Add(submit_button);
    }

Description :-
hi Friends ! Here I have performed following steps in above c# codes as given below:-
  • I have created TextBox control and its event handler dynamically.
  • I have created a Master page  dynamically.
  • I have also checked whether view state property enable in this section or not
  • I have created Button control and its event handler dynamically.
  • I have Showed some value in label control.
I will explain it with an example after finish all events .

2.) Init :- 
  • This event fires after each control has been initialized.
  • This is used to read the property of each initialized control .
  • Set Unique Id  and skin properties of the controls.
  • We can't get the Text Box value due to view state property is false after click  the  Submit Button.
  • In this event, we can't able to get the post back values of the controls. 
Example of Init Event:-

protected void Page_Init(object sender, EventArgs e)
        {
      //Checking whether value is strored From view state or not in Intit Event
            int i = Convert.ToInt32(ViewState["a"]) + 1;
            ViewState["a"] = i.ToString();
       Label1.Text = Label1.Text + "<br/>" + "I am Init Event "+ViewState["a"];
        }

Description :-
  • I have created a view state and stored in i variable to check value is stored from view state or not 
  • Display the value of view state when page post back to the sever.
3.) InitComplete :- 
  • Initialization process will be complete in this section..
  • It is raised by the page object.
  • This is used for event processing task.
Example of InitComplete Events:-

protected void Page_InitComplete(object sender, EventArgs e)
        {
//Checking whether value is strored From view state or not in InitComplete Event
            int i = Convert.ToInt32(ViewState["b"]) + 1;
            ViewState["b"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + " I am InitComplete Event " + ViewState["b"];
        }
Description :-
  • I have created a view state and stored in i variable to check value is stored from view state or not 
  • Display the value of view state when page post back to the sever.
4.) PreLoad :- 
  • In This Event ,From where View state functionality starts retrieving the page controls data (value).
  • In this event,View State Data are loaded  to the controls.
  • In this event ,Post Back data are also handled by the Page contols
  • This Event (PreLoad) always Fired after the InitComplete Event.
Example of PreLoad Event:-

protected override void OnPreLoad(EventArgs e)
        {
            //Page.EnableViewState = false;
   //Checking whether value is strored From view state or not in OnPreLoad Event
            int i = Convert.ToInt32(ViewState["c"]) + 1;
            ViewState["c"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + " I am PreLoad Event " + ViewState["c"];
        }
Description :-
  • I have created a view state and stored in i variable to check value is stored from view state or not 
  • I have Displayed the value  to the label of view state when page post back to the sever.
5.) Load :-
  • It use the On Load event method to set the properties of the controls and establish database connection.
  • The page calls the On Load event method on the page then recursively does the same for each child controls until the  control load and page load . 
  • We can check IsValid Property on the page Load event method.
  • We can Create Dynamic  controls also in this event method.
  • All the page values restored in this method. 
Example of Load Event:-

protected void Page_Load(object sender, EventArgs e)
        {
    //Checking whether value is strored From view state or not in Load Event
            int i = Convert.ToInt32(ViewState["d"]) + 1;
            ViewState["d"] = i.ToString();
     Label1.Text = Label1.Text + "<br/>" + "I am Load Event " + ViewState["d"];
        }

Description :-
  • I have created a view state and stored in i variable to check value is stored from view state or not 
  • I have Displayed the value  to the label of view state when page post back to the sever.
6.) LoadComplete :-
  • It raised at the end of the event handling stage.
  • It use this event for tasks that require that all other controls on the page be loaded.
  • All the event processing has been done in this event method.
Example Of LoadComplete Event:-

protected void Page_LoadComplete(object sender, EventArgs e)
        {
//Checking whether value is strored From view state or not in LoadComplete Event
            int i = Convert.ToInt32(ViewState["e"]) + 1;
            ViewState["e"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am LoadComplete Event " + ViewState["e"];
        }

Description :-
  • I have created a view state and stored in variable to check value is stored from view state or not 
  • I have Displayed the value  to the label of view state when page post back to the sever.
7.) PreRender :-
  • PreRender event method is called when page has  created all the controls and displayed in browser.
  • PreRender method is last place where you can change any things on the page.
  • Any change will be stored on the page.
  • The PreRender event occurs for each controls on the page.
Example of Prerender Event:-

//Checking whether value is strored From view state or not in OnPrerender Event
            int i = Convert.ToInt32(ViewState["f"]) + 1;
            ViewState["f"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am PreRender Event " + ViewState["f"];
        }
Description :-
  • I have created a view state and stored in variable to check value is stored from view state or not 
  • I have Displayed the value  in the label of view state when page post back to the sever.
8.) SaveStateComplete :-
  • Before this events occurs,View State has been saved for the page and for all controls.
  • This event is fired after the complete the PreRenderComplete event.
  • In this event method , if you change the server control then state will not be available in next Post Backs.
  • In this Event Method ,You can do the changes in server control also. 
Example:-

protected override void OnSaveStateComplete(EventArgs e)
        {
     //Checking whether value is strored From view state or not in OnSaveComplete Event
            int i = Convert.ToInt32(ViewState["g"]) + 1;
            ViewState["g"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am SaveStateComplete Event " + ViewState["g"];
        }
Description :-
  • I have created a view state and stored in variable to check value is stored from view state or not 
  • I have Displayed the value  in the label of view state when page post back to the sever.
9.) Render  :-
  • This method (Render ) display the  HTML,DHTML and Scripts codes in control on the  Browser (client side).
  • A user code (.ascx file) automatically incorporates rendering so you will not need to explicitly render the control in the code.
Example:-

protected void Render(HtmlTextWriter writer)
        {
            writer.Write("<html><<h>Hi friends how r u </h></html>");
            base.Render(writer);
        }
Description:-
  • Here,I have override the Render Method in my code.
  • I can see the html text on the browser.
  • If i remove base.Render() method then we can't see this text on the browser.
10.) Unload :-
  • This is the last event that fired on the page.
  • This is the page cleaning process such as Instances cleaning or objects,closing the database connections ,closing the opened file.
  • In this phase you can't do any manipulation.
  • Suppose if you call a method such as Response.write method then page will throw an exception.
Example:-

protected void Page_UnLoad(object sender, EventArgs e)
        {
    //Checking whether value is strored From view state or not in UnLoad Event
            int i = Convert.ToInt32(ViewState["h"]) + 1;
            ViewState["h"] = i.ToString();
     Label1.Text = Label1.Text + "<br/>" + "I am UnLoad Event" + ViewState["h"];
        }


Hi Friends ! Now you can implement your  above theory concepts by an example.There are some steps to implement this concepts as given below:-
Step 1 :- First open your visual studio --> File -->New -->Website --> Select ASP.NET Empty website--OK --> Open Solution Explorer --> Add a New web Form (life_cycle.aspx) --> Drag and drop one label control on the form (or create it by c# coding also) as shown below:-
life_cycle

Step :-  Now Press F 7 --> Write the following c# codes as given below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class life_cycle : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        // Text Box Control Dynamically
        TextBox txt = new TextBox();
        txt.ID = "mytext";
        txt.EnableTheming = true;
        txt.Text = "I am TextBox";
    //When you create below event handler press Tab 2 times from your keyboard
        txt.TextChanged += new EventHandler(txt_TextChanged);
        this.form1.Controls.Add(txt);
        txt.Text = "Change Text&press Submit";

        //Page.MasterPageFile = "~/MasterPage.master";

        //Checking whether value is strored From view state or not in preIntit Event
        int i = Convert.ToInt32(ViewState["p"]) + 1;
        ViewState["p"] = i.ToString();
        Label1.Text = " Hi..";
        Label1.Text = Label1.Text + "<br/>" + " I am PreInit Event " + ViewState["p"];

        // create button control dynamically
        Button submit_button = new Button();
        submit_button.ID ="btntxt";
        submit_button.Text="Submit";
    //When you create below event handler press Tab 2 times from your keyboard
        submit_button.Click +=new EventHandler(submit_button_Click);
        this.form1.Controls.Add(submit_button);
    }
        protected void Page_Init(object sender, EventArgs e)
        {
      //Checking whether value is strored From view state or not in Intit Event
            int i = Convert.ToInt32(ViewState["a"]) + 1;
            ViewState["a"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am Init Event "+ViewState["a"];
        }
        protected void Page_InitComplete(object sender, EventArgs e)
        {
      //Checking whether value is strored From view state or not in InitComplete Event
            int i = Convert.ToInt32(ViewState["b"]) + 1;
            ViewState["b"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + " I am InitComplete Event " + ViewState["b"];
        }

        protected override void OnPreLoad(EventArgs e)
        {
            //Page.EnableViewState = false;
     //Checking whether value is strored From view state or not in OnPreLoad Event
            int i = Convert.ToInt32(ViewState["c"]) + 1;
            ViewState["c"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + " I am PreLoad Event " + ViewState["c"];
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    //Checking whether value is strored From view state or not in Load Event
                int i = Convert.ToInt32(ViewState["d"]) + 1;
                ViewState["d"] = i.ToString();
                Label1.Text = Label1.Text + "<br/>" + "I am Load Event " + ViewState["d"];
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
   //Checking whether value is strored From view state or not in LoadComplete Event
            int i = Convert.ToInt32(ViewState["e"]) + 1;
            ViewState["e"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am LoadComplete Event " + ViewState["e"];
        }

        protected override void OnPreRender(EventArgs e)
        {
     //Checking whether value is strored From view state or not in OnPrerender Event
            int i = Convert.ToInt32(ViewState["f"]) + 1;
            ViewState["f"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am PreRender Event " + ViewState["f"];
        }

        protected override void OnSaveStateComplete(EventArgs e)
        {
     //Checking whether value is strored From view state or not in OnSaveComplete Event
            int i = Convert.ToInt32(ViewState["g"]) + 1;
            ViewState["g"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am SaveStateComplete Event " + ViewState["g"];
        }
       
        protected void Page_UnLoad(object sender, EventArgs e)
        {
    //Checking whether value is strored From view state or not in UnLoad Event
            int i = Convert.ToInt32(ViewState["h"]) + 1;
            ViewState["h"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + "I am UnLoad Event" + ViewState["h"];
            Response.Write("hello ji");//when you run this will show error 
        }
       protected void txt_TextChanged(object sender, EventArgs e)
        {
   //Checking whether value is strored From view state or not in tet_Change Event
            int i = Convert.ToInt32(ViewState["i"]) + 1;
            ViewState["i"] = i.ToString();
            Label1.Text = "I am Text change Event " + ViewState["i"];
        }
        protected void  submit_button_Click(object sender, EventArgs e)
        {
 //Checking whether value is strored From view state or not in Submit_button Event
            int i = Convert.ToInt32(ViewState["j"]) + 1;
            ViewState["j"] = i.ToString();
            Label1.Text = Label1.Text + "<br/>" + " I am Submit_button Event " + ViewState["j"];
          }
       }


Step :-  Now run  the application (press F 5) -->You will see following output as shown below:-
cycle_out
Description:-
  • When we run first time any .NET application then each event call by visual studio compiler sequentially. Here Microsoft developers have created some rule on each event and putted its sequential executions which i have already explained above theory part. 
  • Here you can see each event is calling in sequence manner.If change the positions of any events then there will be no changes in their execution.They will execute according to the compiler rules.
  • You can change event positions from your coding part and see the output yourself.
  • Here page is loaded first time , so Enable View State Properties will be "False" at that time.
  • When page post Back from server then Enable View State properties will be "True".
  • To more about view state Read this post.

Step 4 :-Now Press Submit Button --> You will see following output as shown below:-


submit_output
Description:-  

  • View state functionality starts retrieving the page controls data (value).
  • You can see the repeated values in above example .It shows view state property is Enable for this event methods.  
Step 5 :Now change Text Box Text --> and Press submit button -->you will see following output as shown below:-
change_out
Description:-

  • After change the Text Box and press Submit Button ,you will see above output.
  • Only these event method will be called as shown above.
  • You can see above three event method(preinit,init and initComplete) did not call in this section.

Step 6 :- Now Again Press Submit Button -->then you will see following output as shown below:-
output
Description:-

  • When press Submit button again without changing Text Box Value ,you will see above event methods values.Now you can predict yourself. 

Step 7 :- If you write Response.write() on the page Load then you will show following error as shown below:-
page_error

Description:-
  • You can add Response.redirect () method in any event method section instead on Page_Load section then it will work with no error.
For More... 
  1. How to save image in database and display in picture Box
  2. How to check whether cookie is present in browser or not.
  3. How to Run C# codes on NotePad
  4. Difference between web services and wcf in ASP.NET
  5. How to implement Form based authentication in asp.net application
  6. How to implement caching and ajax in asp.net 
  7. How to send forget password on asp.net website free
  8. What is HTTP and session Management in asp.net
  9. How to implement joins concepts in sql Database
  10. Create composite custom control for your asp.net application
  11. How to create captcha image in new way in asp.net
  12. How to use Navigation control in your asp.net application
  13. How to implement Web Form Controls in asp.net
  14. How to Implement Reflection concepts in c#
  15. How to implement asp.net website on server free
  16. How to use secure login page in asp.net with example
  17. How to Add and verify Captcha image in 3 tier architecture in asp.net
  18. How to use virtual keyboard in asp.net for security purpose
  19. How to implement hashing concepts in asp.net
  20. Learn complete .Net Interview Questions and answers for job seekers
I hope this is helpful for you.
You can share this post with your friends,if you have learn somethings from this post or other posts. If  any confusion (problem),comment it .
Download Whole Attached file
        Download 

1 comment:

Powered by Blogger.