5

I am trying to add a few additional lines of code to the Page_Load method of an ASP.Net page, where the existing Page_Load code is stored in a compiled codebehind DLL. I don't have access to the source for the DLL, although I can extract the code for the Page_Load method using Dis#.

What is the best way to add the new code? I need the existing Page_Load code to execute, together with the new code, and it doesn't matter in what order they execute.

Specifically, I'm fixing an old application that uses the Telerik RadEditor which doesn't work correctly under Firefox 6. See this page for the exact code I'm adding.

6 Answers 6

3

If you can access the ASPX portion, you can try adding a code block to the ASPX, and overriding OnInit or OnPreInit, which should work fine for the code you're trying to add.

<script runat="server">
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        if (Request.Browser.Browser.ToLowerInvariant() == "firefox")
        {    
            System.Reflection.FieldInfo browserCheckedField = typeof(RadEditor).GetField("_browserCapabilitiesRetrieved", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);    
            browserCheckedField.SetValue(RadEditor1, true);    

            System.Reflection.FieldInfo browserSupportedField = typeof(RadEditor).GetField("_isSupportedBrowser", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);    
            browserSupportedField.SetValue(RadEditor1, true);
        }                        
    }
</script>   
1
  • Fantastic, thank you very much for this suggestion. I had to use OnInit instead of OnPreInit, since the SetValue function gave an error of 'Non-static field requires a target' when I used OnPreInit. I guess it was too early in the page life-cycle to run this.
    – philwilks
    Aug 31, 2011 at 7:22
2

You can derive from the other Page class and add a Page.Load event handler:

public class YourPage : TheirPage
{
  public YourPage() { Load += YourPage_Load; }

  void YourPage_Load(object s, EventArgs e) { ... }
}

or even override OnLoad():

protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  ...
}
5
  • If the code-behind is compiled, how could you inject an additional class into the assembly?
    – mclark1129
    Aug 30, 2011 at 18:14
  • You put this class in your own assembly and instantiate it (e.g., visit this page). The other code will be called because it's inherited. Aug 30, 2011 at 18:17
  • You would also need to change the ASPX to set the Inherits/CodeFile attributes to the new page class, no?
    – rtalbot
    Aug 30, 2011 at 18:35
  • If there is an ASPX file available that's pointing to the other class, then yes, the Inherits property would have to be changed to the new Page class. Aug 30, 2011 at 18:42
  • Thanks Mark, this seams like the least hacky way of doing it. However I went with James' suggestion as it seemed a bit simpler.
    – philwilks
    Aug 31, 2011 at 7:25
1

You would need to extract all source, add your code and rebuild the entire dll. There is not a way to do this without rebuilding the original assembly. This will be a problem if the original assembly is strong-named. Otherwise, it's a pain but you should be ok.

4
  • 1
    Plus, if you're charged with maintaining a codebase it's helpful to actually have the source.
    – mclark1129
    Aug 30, 2011 at 18:10
  • I don't think that's true. If he can access the ASPX, he can override OnInit or OnPreInit with an inline code block. Aug 30, 2011 at 18:10
  • Ok, didn't think of that. I would consider this inadvisable and would prefer actually having and being able to change the source. Also, I don't know that OnPreInit is advisable as the control properties could be overwritten by ViewState.
    – rtalbot
    Aug 30, 2011 at 18:30
  • Thanks for the suggestion, but I was really looking for a quick and dirty fix. We're going to be replacing the application in the next few months, and just needed to fix this in the short term.
    – philwilks
    Aug 31, 2011 at 7:23
0

can you possibly hook into PreLoad in the aspx page? Controls are loaded at this point and you should be able to do what you need. Sometimes additional processing is required and you can hook into LoadComplete as well which I think may serve you best?

http://msdn.microsoft.com/en-us/library/system.web.ui.page.preload.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.page.onloadcomplete.aspx

0

If you can extract the code from the DLL by dissasembling it, you can disassociate the code behind the page and reimplement the logic in the markup.

<%@ Page Language="C#" AutoEventWireup="true"  %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    lblTest.Text = "Something here";
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Label ID="lblTest" runat="server" ></asp:Label>
</div>
</form>
</body>
</html>
0

You can derive from the existing class and override any of the rendering methods, such as the OnPreRender() method:

public class Class1 : _Default
{
    protected override void OnPreRenderComplete(EventArgs e)
    {
        base.OnPreRenderComplete(e);
        // add your code here
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.