Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page_Load in C#

Tags:

c#

pageload

I am working with C# web application. I want to know deeply about the page events. Because I thought that the page load event happens first (when a page is requested in browser). But when I tried with commenting the method protected void Page_Load(object sender, EventArgs e) the page get loaded without error.

like image 278
Sudha Avatar asked Apr 30 '13 07:04

Sudha


People also ask

What is Page_Load?

Your web form has a method called Page_Load. This method is called each time your page is loaded. This includes: 1. The first time the page is loaded, e.g., when a user enters the url to it or clicks on a link to it.

What is a Page_Load event?

Page_Load Event This event occurs only after Page_Init event and this event also will raise for every postback operation and in this stage all control properties are loaded with information recovered from view state and control state.

Which page event will use in Page_Load?

Page_Load() method is called after a preLoad event.

What is Pageload in C#?

Load. The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.


3 Answers

off-course your webpage will work even if there is no Page_Load() method. Before a Page_Load() events like PreInit, Init() etc are called. Refer to page life cycle.

Page_Load() method is called after a preLoad event. With Page_Load() you can set default values or check for postBacks etc.

 protected void Page_Load(object sender, EventArgs e)
    {
        int x = 10;
    }

write this and put a break-point on int x = 10; watch sender and e.

like image 136
Newton Sheikh Avatar answered Sep 29 '22 15:09

Newton Sheikh


Every Page object has nine events, most of which you will not have to worry about in your day to day dealings with ASP.NET. The three that you will deal with the most are:

Page_Init
Page_Load
Page_PreRender

They do execute in the order given above so make sure to take that into consideration, especially when building custom controls. The reason you have to keep this in mind is because information might not be available when you expect if you do not deal with it appropriately.

Refer: Life Cycle

like image 29
Linga Avatar answered Sep 29 '22 17:09

Linga


1.Page request

2.Start

3.Initialize

4.Load

5.Postback Event Handling

6.Rendering

7.Unload

This is the page life cycle.

Load event comes at 4th position.

You can check details over here:

http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx

like image 28
Freelancer Avatar answered Sep 29 '22 16:09

Freelancer