Execute same code on every WebForm

Have you ever encountered the need to execute lines of code on each page_load event for each page you have in your project! I have seen a lot of developers from the official ASP.NET forums writting the same code on each page which works fine but in case you need to change something you have to reflect it on all the pages.

Below i will describe a way you can implement to overcome this issue.

  1- Create a class "MyPage" which inherits from Page class.

      public partial class MyPage: Page
 
  2- Override the onLoad event of the Page class by adding the below code
   
        protected override void OnLoad(EventArgs e)
        {
            Response.Write("hello World");
            base.OnLoad(e);
        }


  3- In your webform replace System.Web.UI.Page by MyPage

 

Hope this helps,

Published 02 September 2008 10:52 AM by haissam

Comments

# BasharKokash said on 02 September, 2008 06:43 AM

excellant, thanks

# kaushalparik said on 03 September, 2008 12:38 AM

nice Tips, thanx

# Recent Faves Tagged With "writting" : MyNetFaves said on 24 October, 2008 02:35 PM

Pingback from  Recent Faves Tagged With "writting" : MyNetFaves

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP