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,

Comments

# re: Execute same code on every WebForm

Tuesday, September 02, 2008 6:43 AM by BasharKokash

excellant, thanks

# re: Execute same code on every WebForm

Wednesday, September 03, 2008 12:38 AM by kaushalparik

nice Tips, thanx

# Recent Faves Tagged With "writting" : MyNetFaves

Friday, October 24, 2008 2:35 PM by Recent Faves Tagged With "writting" : MyNetFaves

Pingback from  Recent Faves Tagged With "writting" : MyNetFaves

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.