Wednesday 30 June, 2010

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,

No comments:

Post a Comment