Calling the original Page_Load function from inline code

I like the Monkey patch of an ASPX site so that I can add material to the Page_Load method in the assembly assembly.

My first thought was to add a script tag containing the second Page_Load method for the .aspx file, for example:

<script language="CS" runat="server"> void Page_Load(object sender, System.EventArgs e) { // do some stuff in addition to the original Page_Load method } </script> 

But it seems that only the Page_Load method from the embedded code will be executed, and not the one that is in the source code file (in the assembly).

Is it possible to call the source method from my inline code? Or is there another way to add material that should start immediately after calling the Page_Load method from inline code without modifying the existing assembly?

+8
code-behind inline-code
source share
4 answers

The asp.net model is that the page declared in the .aspx file is a subject class of descendants of the class that inherits from System.Web.UI.Page declared in the .aspx.cs file.

So, the Page_Load method is called because it basically hides the original Page_Load method. Following this logic, you can do:

 <script language="CS" runat="server"> void Page_Load(object sender, System.EventArgs e) { base.Page_Load(sender, e); // do some stuff in addition to the original Page_Load method } </script> 

There is no problem with accessibility, since asp.net by default declares Page_Load and similar methods as protected , so descendant classes can call them.

+10
source share

Yes this:

 void Page_Load(object sender, System.EventArgs e) { // Do some things before calling the original Page_Load base.Page_Load(sender, e); // Do some things after calling the original Page_Load } 

The reason for this is because the ASP.Net structure works with a model in which the .aspx file is compiled into a class that inherits from the class defined in your code behind the file (in fact, the class defined by the Inherits attribute on the page tag)

 <%@ Inherits="WebApplication1._Default" ... 
+3
source share

You can also try using the PreLoad method. Those get called before Page_Load and can be a cleaner way of handling things.

0
source share

This works for me.

 <script language="CS" runat="server"> protected override void OnLoad(EventArgs e) { base.OnLoad(e); Response.Write("additional stuff"); } </script> 
0
source share

All Articles