简体   繁体   English

ASP.Net:在代码隐藏的基础上,向Page_Load添加其他代码

[英]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. 我正在尝试向ASP.Net页面的Page_Load方法添加一些额外的代码行,其中现有的Page_Load代码存储在已编译的代码隐藏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#. 虽然我可以使用Dis#提取Page_Load方法的代码,但我无法访问DLL的源代码。

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. 我需要现有的Page_Load代码与新代码一起执行,并且它们执行的顺序无关紧要。

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. 具体来说,我正在修复一个使用Telerik RadEditor的旧应用程序,该应用程序在Firefox 6下无法正常工作。请参阅此页面以获取我正在添加的确切代码。

You can derive from the other Page class and add a Page.Load event handler: 您可以从其他Page类派生并添加Page.Load事件处理程序:

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

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

or even override OnLoad() : 甚至覆盖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. 您需要提取所有源代码,添加代码并重建整个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. 如果您可以访问ASPX部分,您可以尝试将代码块添加到ASPX,并覆盖OnInit或OnPreInit,这对于您尝试添加的代码应该可以正常工作。

<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? 你可以在aspx页面挂钩PreLoad吗? 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? 有时需要额外的处理,你可以挂钩到LoadComplete,我认为这可能是最好的服务吗?

http://msdn.microsoft.com/en-us/library/system.web.ui.page.preload.aspx 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 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. 如果您可以通过对其进行分解来从DLL中提取代码,则可以取消关联页面后面的代码并重新实现标记中的逻辑。

<%@ 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: 您可以从现有类派生并覆盖任何呈现方法,例如OnPreRender()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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