Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to skip calling Page_Load event

0.00/5 (No votes)
13 Jun 2012 1  
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);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here