Click here to Skip to main content
15,885,651 members
Articles / Web Development / ASP.NET
Tip/Trick

How to skip calling Page_Load event

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
13 Jun 2012CPOL 21.8K   3   3
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. 

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Nagarro
India India
I am a senior software engineer in Noida. Primarly worked in .NET,SilverLight, Prism, ASP.NET, C# etc.

Comments and Discussions

 
GeneralMy vote of 5 Pin
nazishrizvi11-Jun-13 17:44
nazishrizvi11-Jun-13 17:44 
GeneralMy vote of 5 Pin
bhargavpp17-Sep-12 19:46
bhargavpp17-Sep-12 19:46 
QuestionAutoEventWireup="false" Pin
giammin13-Jun-12 6:21
giammin13-Jun-12 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.