我可以自动附加到任何支持服务器的 HTML 标记的生命周期吗?
如果“启用服务器”的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是使用适配器完成的事情。您创建一个可以发挥其魔力的应用程序,并将其关联到 App_Browsers 中包含的浏览器文件中。
的示例
这是我的实验性 App_Browsers/Default.browser和我的适配器
...使用 System.Web.UI;
使用 System.Web.UI.Adapters;
我的高度先进的适配器具有超脆弱的能力,可以围绕从 HtmlControl 派生的所有控件(带有 runat="server" 的 html 标签,包括
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
And my adapter...
using System.Web.UI;
using System.Web.UI.Adapters;
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.
以下是页面生命周期文章的链接。这是需要了解的重要信息。
如果您正在对其进行编码,那么您可以创建一个类并覆盖 PreRender,在其中执行您想要的任何操作。然后您的控件将实现该类。
如果您更喜欢更通用的方法,那么您可以在页面级别执行相同的操作。您可以在页面级别挂钩 OnPreRender:
这应该使您能够在渲染之前检查每个控件。
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:
That should give you the ability to check each control just before rendering.
我想这就是我正在寻找的。它递归地迭代整个控件树,并在找到 HtmlControl 时绑定事件处理程序。
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.