简体   繁体   中英

ASP.Net: Add additional code to Page_Load, on top of what is in codebehind

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.

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);
  ...
}

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.

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>   

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

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>

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM