How to: Override the Page_Load function without modifying the original code behind.

Submitted 9/1/2010 by Josh Anderson 1 out of 1 users found this topic helpful

In order to make upgrading to new versions much eaiser , I prefer to add usercontrols, custom classes and overrides within the aspx pages vs customizing the API.
This is an example of how to override the Page_Load in the code behind yet still have the original fire also.

In the code view of a .aspx page, (Not the Code Behind) simply add this to the page for overriding the Page_Load event.

Add this:

<script language="C#" runat="server">

//This will fire on the Page_Load event first allowing you to do custom actions before or after the base function.

new void Page_Load(object sender, EventArgs e)

{

//The following line calls the code behind Page_load, you can remove it if you didnt want it to fire at all.

base.Page_Load(sender, e);

if (CurrentUserSession != null) return;

//Add your custom code here, call functions / methods / etc from your custom classes in the app_code folder
//You can manipulate custom usercontrols
// In this case we are remmoving the visibility of the default links before a user is logged in (I still wanted my custom pages in the nav so this was the best way)

if (CurrentUserSession != null) return;

this.tdVideos.Visible = false;

this.tdGroups.Visible = false;

this.tdSearch.Visible = false;

this.tdTopCharts.Visible = false;

}

</script>

There are debates about inline code and performace, however, the bottom line is that, if done properly, you will not see a performance hit.