我可以自动附加到任何支持服务器的 HTML 标记的生命周期吗?

发布于 2024-10-09 14:24:13 字数 290 浏览 6 评论 0原文

如果“启用服务器”的 HTML 标记位于 Web 表单中,就像这样 --

<p runat="server"/>

-- 有什么方法可以让我附加到它的渲染吗?我假设一旦它们运行了 runat="server",它们就必须有某种生命周期。

我想将一些代码附加到启用的任何 HTML 标记的渲染中。因此,每当模板作者将 runat="server" 放在标签上时,我就可以捕获 PreRender(或其他任何内容)并执行一些代码。

可能的?

If a "server-enabled" HTML tag is in a Web form, like this --

<p runat="server"/>

-- is there any way for me to attach to its rendering? I assume once they have runat="server", they must have a lifecycle of some kind.

I'd like to attach some code to the rendering of any HTML tag so enabled. So, whenever a template author puts runat="server" on a tag, I can catch the PreRender (or anything else) and execute some code.

Possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-10-16 14:24:13

这是使用适配器完成的事情。您创建一个可以发挥其魔力的应用程序,并将其关联到 App_Browsers 中包含的浏览器文件中。

的示例

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.HtmlControls.HtmlControl"
               adapterType="App_Code.Adapters.HtmlControlAdapter" />
    </controlAdapters>
  </browser>
</browsers>

这是我的实验性 App_Browsers/Default.browser和我的适配器

...使用 System.Web.UI;
使用 System.Web.UI.Adapters;

using System.Web.UI;
using System.Web.UI.Adapters;

namespace App_Code.Adapters {
    public class HtmlControlAdapter : ControlAdapter {
        protected override void Render(HtmlTextWriter writer) {
            writer.Write("<div style='background-color: #f00;'>");
            base.Render(writer);
            writer.Write("</div>");
        }
    }
}

我的高度先进的适配器具有超脆弱的能力,可以围绕从 HtmlControl 派生的所有控件(带有 runat="server" 的 html 标签,包括

)呈现具有内联样式的 div。您的适配器可以挂钩由控件触发的任何事件,因此这应该可以满足您的需求。

This is something done using adapters. You create one that does its magic, and associated it in a browser file contained in App_Browsers.

Here's an example of my experimental App_Browsers/Default.browser

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.HtmlControls.HtmlControl"
               adapterType="App_Code.Adapters.HtmlControlAdapter" />
    </controlAdapters>
  </browser>
</browsers>

And my adapter...

using System.Web.UI;
using System.Web.UI.Adapters;

using System.Web.UI;
using System.Web.UI.Adapters;

namespace App_Code.Adapters {
    public class HtmlControlAdapter : ControlAdapter {
        protected override void Render(HtmlTextWriter writer) {
            writer.Write("<div style='background-color: #f00;'>");
            base.Render(writer);
            writer.Write("</div>");
        }
    }
}

My highly advanced adapter with superfragilicious abilities renders a div with inline styles around all controls deriving from HtmlControl (html-tags with runat="server", including <form runat="server">). Your adapter can hook into any event triggered by the control, so this should solve your needs.

梦境 2024-10-16 14:24:13

以下是页面生命周期文章的链接。这是需要了解的重要信息。

如果您正在对其进行编码,那么您可以创建一个类并覆盖 PreRender,在其中执行您想要的任何操作。然后您的控件将实现该类。

如果您更喜欢更通用的方法,那么您可以在页面级别执行相同的操作。您可以在页面级别挂钩 OnPreRender:

private void Page_PreRender(object sender, System.EventArgs e)
{
    Page page = sender as Page;
    if (page != null)
    {
        page.Controls.Clear(); // Or do whatever u want with ur page...
    }
}

private void InitializeComponent()
{    
    // Handle the Page.PreRender event.
    this.PreRender += new System.EventHandler(this.Page_PreRender);
}

这应该使您能够在渲染之前检查每个控件。

Here is a link to the page lifecycle article. It's an important piece of information to know.

If you are coding it then you could create a class and override PreRender doing whatever you want inside it. Then your controls would implement that class.

If you prefer a more generic approach then you could do the same thing but at the page level. You could hook OnPreRender at the page level as such:

private void Page_PreRender(object sender, System.EventArgs e)
{
    Page page = sender as Page;
    if (page != null)
    {
        page.Controls.Clear(); // Or do whatever u want with ur page...
    }
}

private void InitializeComponent()
{    
    // Handle the Page.PreRender event.
    this.PreRender += new System.EventHandler(this.Page_PreRender);
}

That should give you the ability to check each control just before rendering.

千寻… 2024-10-16 14:24:13

我想这就是我正在寻找的。它递归地迭代整个控件树,并在找到 HtmlControl 时绑定事件处理程序。

protected override void OnLoad(System.EventArgs e)
{
    base.OnLoad(e);
    BindTagProcessor(Page);
}

private void BindTagProcessor(Control control)
{
    foreach (Control childControl in control.Controls)
    {
        if (childControl is HtmlControl)
        {
            ((HtmlControl)childControl).PreRender += new EventHandler(MyTagProcessor);                   
        }
        BindTagProcessor(childControl);
    }
}

I think this is what I'm looking for. It recursively iterates the entire control tree, and binds the event handler when it finds a HtmlControl.

protected override void OnLoad(System.EventArgs e)
{
    base.OnLoad(e);
    BindTagProcessor(Page);
}

private void BindTagProcessor(Control control)
{
    foreach (Control childControl in control.Controls)
    {
        if (childControl is HtmlControl)
        {
            ((HtmlControl)childControl).PreRender += new EventHandler(MyTagProcessor);                   
        }
        BindTagProcessor(childControl);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文