Tuesday, April 9, 2013

Page Life Cycle


PreInit 
PreInit is the first phase in asp.net page life cycle events. This is the place where one can dynamically set their master page in the code. In such case you can also set any theme or skin to your page, in PreInit we can also set the properties of the server controls of Master page, like the code below:

protected void Page_PreInit(object sender, EventArgs e)
{
if (textBox1 == null)
       {
          Response.Write("Pre Init, server control Not available");
       }
       else
       {
          string TextinInit = textBox1.Text;
          Response.Write("Pre Init, Server Control enabled");
       }

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

       TextBox _mstTextBox = (TextBox)Page.Master.FindControl("txtMessage");
       Label _mstLabel = (Label)Page.Master.FindControl("lblMessage");

       _mstLabel.Text = "Setted in default page at time : " +ateTime.Now.ToString();
       _mstTextBox.Text = "Textbox set in default page";
  }

In above you can see I’ve used a master page dynamically and set a label and TextBox’s Text property of that master page in runtime that means we can set the master page dynamically and associate the values in their controls if any. If you try to set the masterpage in any other event after Page_PreInit event, you will get an error message that will tell you that the master page only can be set on or before Page_PreInit event. In Page_PreInit event you can also see at the top level I’m checking a TextBox1 value whether it’s null or not. It is a server control (TextBox), but in PreInit the value will always be null of this text box or any other server control. So if I want to conclude the thing, in Page_PreInit controls are not fully initialized. So use this event for setting MasterPage file in your code. Each control Unique ID’s are also available at this event.

Init  
Init is called when you can initialize your page controls, this is the place that comes into picture after each Control initialization, till this event they get their Unique Id, master page etc. In Init you cannot access any controls properties after the last viewstate, I mean suppose you enter some text in a textbox control and click on a button control that makes a postback, now you want to view the value latest entered. In such case you will be able to see the textbox value which you’ve entered after the round trip happens, this is because of ViewState property of controls. That means they preserved your last saved value, but you see this value after Init phase because, usually we don’t use these events in our code and use just Page_Load event where the Viewstate value already got loaded. But as in Init phase, we cannot access our latest entered values in textbox, so in Init you won’t be able to get the latest changes. So I can say in Init we won’t be able to get the postback value.

InitComplete
In this event, Viewstate functionality is turned on for server control. Viewstate of controls allows preserving their values across postback.

PreLoad
This is the first event where viewstate functionality starts retrieving their values. PreLoad executes after InitComplete method. In this method, page has loaded values from viewstate. So for example in a button click event if you create a viewstate like below than what will going to be happen:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ViewState["myData"] = "just a text string";
        }

After button click, postback will happen at it will start calling page events like PreInit, Init, Initcomplete. Till these three events you will not be able to get the value of Viewstate “myData”. Even when the event fires PreLoad event of page, at this time you cannot access the ViewState, this is because ViewState will set the value when asp.net reaches the ButtonClick event, and this will happen after Load event, so one more time we will click on our submit button, and because now the ViewState[“myData”] resides into my page, I can access it on my PreLoad event. For better understanding please see the screen shot below:
ASP .Net page life cycle

Load
Most of the developers are familiar with the page load, as this is the only event that comes by default in aspx.cs page when you start doing anything with your code behind. The page Load events executes after the Page_PreLoad event. If you have created your own Load method in constructor of your code behing, then this will be the method that will execute before the Page_Load event and after Page_PreLoad event, for better understanding refer to the image below:
Init event in Page Life Cycle


As you are seeing in above code, I’ve created my own Page_Load event in my code behind as well as the default Load event, I didn’t mention Page_PreLoad event here, but it will execute after Page_InitComplete event. So back to the image, the _Default_Load will execute first before the Page_Load event. If you have any master page in your page, then the Load event of your master page will run followed by the user control Load event if they exist in your file.

Events
After the Page_load the next event is Page Control’s event. For example, if you have a control that raises postback just like button, and you clicked on the Button, so after Page_Load your Button_Click event will fire, and do the rest of the thing you’ve mentioned in your page. for better understanding please refer to the image below, event execution will be happen in that sequence how image is showing, in event portion whatever you will do that will go ahead and will do the things you’ve mentioned in you page. just like below, I said to create a ViewState[“myData”] and put some string inside that viewstate. So now the viewstate is ready to preserve ahead in my code. 

In Events you can also check the Page.IsValid property if you’ve used any validator controls in your page like regularexpressionValidator, requiredFieldValidator, Range etc. To check this property refer to the image below.
Load complete event of Page Life Cycle


LoadComplete
This event can be used when all the event processing has been done in the page.

PreRender/PreRenderComplete 

PreRender event gets called when page has created all the controls that user will see in his browser. PreRenderComplete is the last place to change anything in the page. After this event any changes will not get preserved in page cycle. 

SaveStateComplete
event gets fired immediately after the PreRenderComplete event, in this event the viewstate functionality and control state get saved, as I said in PreRenderComplete changes can be done that will affect the next postback. In SaveStateComplete you can do the changes in your server control, but the state won’t be available in next postbacks. 

Render 
Render is not an event but this is the phase where the HTML of your page will get rendered to the output stream with the help of HTMLTextWriter. You can override the Render method of page if you want to write your own code rather than the actual HTML text. As like below:

Render event of Page Life Cycle

As you can see in the code above, I’ve override the Render method in my code and write something with Heading 1 tag and later on called the base.render method. In current scenario the thing will happen is: apart my server controls code in my browser I’ll be able to see my Hello world text also. If I remove the base.render method calls, than I won’t be able to view my any control if I’ve created in page.


Unload 
This is the last event that gets fired. This is the page cleaning process like closing the open file connections etc, so in this process you cannot do any kind of manipulation with data that affect the rendering, you are restricted to use Response property also, and doing such you will get an exception message.

There might be some mistakes with this article but I tried my best to share with you, So if you have some queries OR suggestions please feel free and share the by comment. Thank you for reading this post..
- See more at: http://www.codeimagine.com/2012/09/define-page-life-cycle-events-asp-net.html#sthash.L23gBcGB.dpuf

67 comments:

  1. I ѕimply coulԁ nοt deρart yοur
    wеbsitе prior tο ѕuggestіng that I ехtremely loved the uѕuаl informаtion а peгѕon provіԁe foг your vіsіtоrs?
    Is going tо bе again regularly to сheck
    out new postѕ

    Alsο νisit my homeрage Free Ms Points

    ReplyDelete
  2. I love your blog.. very nice colors & theme.

    Did you design this website yourself or did you hire someone to do it for you?
    Plz respond as I'm looking to design my own blog and would like to find out where u got this from. kudos

    Have a look at my webpage - Marketing program

    ReplyDelete
  3. Hiya! I just want to give a huge thumbs up for the great data you
    could have here on this post. I can be coming again to your weblog for extra soon.



    Have a look at my web site show me your tongue ring

    ReplyDelete
  4. Thankyou for all the information. I also found a link with free GATE CSE testpapers that you can practice online. Here is the link: http://thegateacademy.com/gate-free-test/

    ReplyDelete
  5. Really very informative data is given in the post.It will surely help the Gate students.You can also visit to our website for gate class in Jaipur for more info.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hello guys , Welcome to Escorts Agency in various beautiful cities the all new Escort index devoted to transferrable you the best that the net must provide to Escort Services in city center. We have listed the best native city center Escorts . These women square measure clever and well refined Independent Escorts. Udaipur Escorts, Jaipur Escorts, Udaipur Escorts, Ajmer Escorts, Udaipur Escorts, Jodhpur Escorts, Udaipur Escorts, Bikaner Escorts, Udaipur Escorts, Kota Escorts, Udaipur Escorts, Jodhpur Escorts, Kota Escorts, Udaipur Escorts, Jaipur Escorts, Banswara Escorts, Surat Escorts, Chandigarh Escorts, Goa Escorts, Gurgaon Escorts, Udaipur Escorts, Ahemdabad Escorts, Gandhinagar Escorts, Jalandhar Escorts, Jodhpur Escorts, Jamnagar Escorts, Junagadh Escorts

    ReplyDelete
  8. For those who seeking Property in Gurgaon for buy, sell and rent on Ghareli.com

    ReplyDelete
  9. Thanks for all the information.

    ReplyDelete
  10. Hello Udaipur
    I am Preeti Singh- a special escorts girl in Udaipur. I am Providing Russian & Indian Model escorts in Udaipur. We have a large gallery of Call girls in Udaipur for your physically satisfaction. Do you want to more fun and enjoy with me any my escort girls then you simply visit my websites and Book me Now:-

    http://www.russianmodels.in
    http://www.rjescort.co.in/udaipur-escort
    http://www.mahiescort.in/udaipur-escort
    http://www.jeshika.com/udaipur-escort

    Anchor Text

    Udaipur Escorts
    Udaipur Call Girls
    udaipur Escorts
    udaipur Call Girls
    Udaipur Escorts
    Udaipur Call Girls
    Udaipur Escorts
    Udaipur Call Girls
    Ajmer Escorts
    Ajmer Call Girls
    Pushkar Escorts
    Pushkar Call Girls

    ReplyDelete
  11. Hello Guys...
    I am Preeti Singh-a hot and sexy escort girl. I am a Top Class Model Girl and I am providing Special Escorts Services in all major cities by Me and My Hot and Sexy Call Girls. I am offering Many Types Escort Services with fully satisfaction and Relaxation without any doubt. So If you want to Fully satisfaction then visit my website and book me any my Escorts Gallery Hot Model soon as soon and enjoy your memorable moments.

    Ajmer Escorts
    Ajmer Call Girls
    Pushkar Escorts
    Pushkar Call Girls
    Ajmer Escorts
    Ajmer Call Girls
    Ajmer Escorts
    Ajmer Call Girls
    Jaipur Escorts
    Jaipur Call Girls
    Udaipur Escorts
    Udaipur Call Girls

    ReplyDelete
  12. Hello Guys...
    I am Preeti Singh-a hot and sexy escort girl. I am a Top Class Model Girl and I am providing Special Escorts Services in all major cities by Me and My Hot and Sexy Call Girls. I am offering Many Types Escort Services with fully satisfaction and Relaxation without any doubt. So If you want to Fully satisfaction then visit my website and book me any my Escorts Gallery Hot Model soon as soon and enjoy your memorable moments.

    Ajmer Escorts
    Ajmer Escorts Service
    Ajmer Call Girls
    Escorts in Ajmer
    Escort Service in Ajmer
    Call Girls in Ajmer

    Pushkar Escorts
    Pushkar Escorts Service
    Pushkar Call Girls
    Escorts in Pushkar
    Escort Service in Pushkar
    Call Girls in Pushkar

    Jodhpur Escorts
    Jodhpur Escorts Service
    Jodhpur Call Girls
    Escorts in Jodhpur
    Escort Service in Jodhpur
    Call Girls in Jodhpur

    Udaipur Escorts
    Udaipur Escorts Service
    Udaipur Call Girls
    Escorts in Udaipur
    Escort Service in Udaipur
    Call Girls in Udaipur

    Udaipur Escorts
    Udaipur Escorts Service
    Udaipur Call Girls
    Escorts in Udaipur
    Escort Service in Udaipur
    Call Girls in Udaipur

    ReplyDelete
  13. Great article on some very useful tools, some of which I use every day. Hindi Love Quotes Thank you so much for the detailed information. Hindi Life Status The information in this article is very helpful to me. Thanks a lot for sharing. Love Shayari in Hindi Keep blogging.

    ReplyDelete
  14. Welcome to Ajmer Escort Service. Myself Mahi Sharma a hot and sexy Call Girl in Ajmer. I am providing Luxury Escort Service in Ajmer by Top Class Models.
    http://www.rjescort.co.in
    https://www.quora.com/profile/Ajmer-Escorts-1
    http://www.jeshika.com/ajmer-escort
    http://www.rjescort.co.in/pushkar-escorts
    http://www.rjescort.co.in/kishangarh-escorts
    http://www.rjescort.co.in/beawar-escorts
    http://www.bookmybabes.info/ajmer-escorts
    http://www.jaipurmodelescort.com/ajmer-escorts-call-girls

    Ajmer Escorts
    Ajmer Escort Service
    Ajmer Call Girls
    Escort Service in Ajmer
    Call Girls in Ajmer

    Pushkar Escorts
    Pushkar Escort Service
    Pushkar Call Girls
    Escort Service in Pushkar
    Call Girls in Pushkar

    Ajmer Escorts
    Ajmer Escort Service
    Ajmer Call Girls
    Escort Service in Ajmer
    Call Girls in Ajmer

    Ajmer Escorts
    Ajmer Escort Service
    Ajmer Call Girls
    Escort Service in Ajmer
    Call Girls in Ajmer

    Ajmer Escorts
    Ajmer Escort Service
    Ajmer Call Girls
    Escort Service in Ajmer
    Call Girls in Ajmer

    Ajmer Escorts
    Ajmer Escort Service
    Ajmer Call Girls
    Escort Service in Ajmer
    Call Girls in Ajmer

    ReplyDelete
  15. Welcome to World's Sex Heaven "Jaipur Escorts" Service. We have world's most beautiful girls who gives Luxury and Delicious Escort Service in Jaipur. Our Escort angels are very hot and sexy. Our Jaipur Escort Service are very comfortable. If you want to take special fun with My Sex Heaven Angles then visit my websites and book your dream girl and take fully chargable fun.



    Our Escorts Keywords:-

    Jaipur Escorts
    Jaipur Escorts Service
    Jaipur Call Girls
    Escorts in Jaipur
    Escort Service in Jaipur
    Call Girls in Jaipur

    Jaipur Escorts
    Jaipur Escorts Service
    Jaipur Call Girls
    Escorts in Jaipur
    Escort Service in Jaipur
    Call Girls in Jaipur

    Jaipur Escorts
    Jaipur Escorts Service
    Jaipur Call Girls
    Escorts in Jaipur
    Escort Service in Jaipur
    Call Girls in Jaipur

    Jaipur Escorts
    Jaipur Escorts Service
    Jaipur Call Girls
    Escorts in Jaipur
    Escort Service in Jaipur
    Call Girls in Jaipur

    ReplyDelete
  16. Welcome to India's Largest Escorts Agency in Udaipur. We Providing Luxury Escort in Udaipur. I have many top models in Udaipur for Escorts. I also have Top Collage girls, Charming Housewives, Models, Air-hostess, Indian-Aunty, Russian Call Girls etc. We providing Many types escort services in Udaipur. Real Fun with Udaipur Escort Service. If you want to more fun with my dream girls then visit my websites and book me for whole night today.

    Udaipur Escorts
    Udaipur Escorts Service
    Udaipur Call Girls
    Escorts in Udaipur
    Escort Service in Udaipur
    Call Girls in Udaipur

    Udaipur Escorts
    Udaipur Escorts Service
    Udaipur Call Girls
    Escorts in Udaipur
    Escort Service in Udaipur
    Call Girls in Udaipur

    ReplyDelete
  17. Welcome to Udaipur Escorts Service
    Welcome to India's Largest Escorts Agency in Udaipur. We Providing Luxury Escort in Udaipur. I have many top models in Udaipur for Escorts. I also have Top Collage girls, Charming Housewives, Models, Air-hostess, Indian-Aunty, Russian Call Girls etc. We providing Many types escort services in Udaipur. Real Fun with Udaipur Escort Service. If you want to more fun with my dream girls then visit my websites and book me for whole night today.

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    JAIPUR ESCORT
    JAIPUR ESCORTS
    JAIPUR ESCORT SERVICE
    JAIPUR CALL GIRLS

    JAIPUR ESCORT
    JAIPUR ESCORTS
    JAIPUR ESCORT SERVICE
    JAIPUR CALL GIRLS

    INDORE ESCORT
    INDORE ESCORTS
    INDORE ESCORT SERVICE
    INDORE CALL GIRLS

    AJMER ESCORT
    AJMER ESCORTS
    AJMER ESCORT SERVICE
    AJMER CALL GIRLS

    AJMER ESCORT
    AJMER ESCORTS
    AJMER ESCORT SERVICE
    AJMER CALL GIRLS

    ReplyDelete
  18. Welcome to Udaipur Escorts Service
    Welcome to India's Largest Escorts Agency in Udaipur. We Providing Luxury Escort in Udaipur. I have many top models in Udaipur for Escorts. I also have Top Collage girls, Charming Housewives, Models, Air-hostess, Indian-Aunty, Russian Call Girls etc. We providing Many types escort services in Udaipur. Real Fun with Udaipur Escort Service. If you want to more fun with my dream girls then visit my websites and book me for whole night today.

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    JAIPUR ESCORT
    JAIPUR ESCORTS
    JAIPUR ESCORT SERVICE
    JAIPUR CALL GIRLS

    JAIPUR ESCORT
    JAIPUR ESCORTS
    JAIPUR ESCORT SERVICE
    JAIPUR CALL GIRLS

    INDORE ESCORT
    INDORE ESCORTS
    INDORE ESCORT SERVICE
    INDORE CALL GIRLS

    AJMER ESCORT
    AJMER ESCORTS
    AJMER ESCORT SERVICE
    AJMER CALL GIRLS

    AJMER ESCORT
    AJMER ESCORTS
    AJMER ESCORT SERVICE
    AJMER CALL GIRLS

    ReplyDelete


  19. http://www.russianmodels.in
    http://www.russianmodels.in/about-me
    http://www.jeshika.com
    http://www.jeshika.com/ajmer-escort
    http://www.jaipurmodelescort.com/ajmer-escorts-call-girls

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    ReplyDelete


  20. http://www.russianmodels.in
    http://www.russianmodels.in/about-me
    http://www.jeshika.com
    http://www.jeshika.com/ajmer-escort
    http://www.jaipurmodelescort.com/ajmer-escorts-call-girls

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    UDAIPUR ESCORT
    UDAIPUR ESCORTS
    UDAIPUR ESCORT SERVICE
    CALL GIRLS IN UDAIPUR
    UDAIPUR CALL GIRLS

    ReplyDelete
  21. When you buy a new HP printer, you try to connect it using the user manual. The manual provided by the 123.hp.com/setup manufacturer may not be in detail. Hence, our technical experts have given simple instructions that guide you through the HP Printer setup and troubleshooting issues if any. We also offer free 123hp driver (Both Basic and Full-feature) to make your printer function efficiently. The navigation is too easy such that all instructions are available on one single page. www.123.hp.com/setup

    ReplyDelete
  22. Amazing nice work. This is very useful article. Thank you. For more

    ReplyDelete
  23. looking for office (Office.com/setup) product or any antivirus like norton (norton.com/setup) or mcafee (McAfee.com/activate) then go through my website

    ReplyDelete
  24. QuickBooks Tech Support Phone Number - If you are looking for a top-grade Accounting and Financial Management software, QuickBooks should be your first choice. It is the most recommended software by Accountants and Bookkeepers all over the world. One of the core reason behind this popularity is the 24×7 available QuickBooks Support services from the toll-free QuickBooks Tech Support Phone Number. QB is highly trustable software and it seldom runs into errors and issues. If in an event, any error pops-up on your screen, simply, dial the toll-free QuickBooks Support Number. Your call will be picked up by an Intuit Certified QuickBooks ProAdvisor who is certified to provide services from the toll-free QuickBooks Support Phone Number.

    Canon Printer Tech Support Phone Number | HP Printer Tech Support Phone Number | Norton Tech Support Phone Number

    ReplyDelete
  25. Amazing nice work. This is very useful article. Thank you. For more

    ReplyDelete
  26. Let Experts Help You With Any Problem With Binance Software

    Call 1833-464-7652 if you are unable to eliminate errors that occur in the Binance account all of sudden? The majority of users are unable to deal with such errors and to get assistance directly from the experts, you can always ask for guidance from the skilled team who is always ready to guide you. Whenever you are in trouble, you can always call on Binance support number which is always functional and the team is ready to guide you. Contact the team for providing results that are easy to execute and start your trading process on the exchange again without facing any trouble.

    ReplyDelete
  27. You are at top class Dehradun escorts agency so there will not be any trouble while taking pleasure because we have a finest procedure before taking pleasure so just feel relax. escort service in Dehradun

    ReplyDelete
  28. thank you very mcuh for this useful info...

    ReplyDelete
  29. Nice Post!
    Sir can you post a guide about pms exam 2021 on your site it will be nice of you….

    ReplyDelete
  30. Various dapper models, hot air women, and surprising wedded ladies are giving escort benefits autonomously and have made popularity among the neighborhood desire searchers. The vast majority of them have their own place to invite you and you can appreciate them with no undesirable unsettling influence.
    mountabu escorts
    kolkata escorts
    kolkata-call-girls-whatsapp-number-escorts
    purulia escorts
    uttar-dinajpur escorts

    ReplyDelete
  31. I am not technically very sound didn’t know how to get rid of the errors and troubles, this post given here helped me a lot.SMS API

    ReplyDelete
  32. replica bags reddit go to my site e2x58t8o64 replica bags aaa quality like it b4y93l8e69 Ysl replica handbags replica bags ebay t6s95r0y85 replica kipling bags browse around this site q8x82j6s46 replica bags

    ReplyDelete