65.9K
CodeProject is changing. Read more.
Home

How to skip calling Page_Load event

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (5 votes)

Jun 13, 2012

CPOL
viewsIcon

22495

Difference between Page_Load and OnLoad event

Introduction

In this article I am going to explain the difference between Page_Load event and OnLoad event. After you have understanding on this you will get the answer of the question "How to skip calling Page_Load" event.

Background 

OnLoad event is responsible to call Page_Load event of our page. The OnLoad function is a virtual function in the System.Web.UI.Page class so we need to override this function if we want to provide a different implementation of this function. So in page Life cycle, first OnLoad executes which in turn calls the Page_Load function. When we override the "OnLoad" function in our class, we need to call the base.OnLoad() function which in turn calls the Page_Load event. If we skip calling/writing base.OnLoad(), then our application simply won't call the Page_Load event.

Using the code 

Below is the code snippet which upon execution will skip the Page_Load event. 

using System.Web; 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "Hello World";            
    }

    protected override void  OnLoad(EventArgs e)
    {
        //  base.OnLoad(e);
    }
}