如何在渲染页面时对其进行操作?

发布于 2024-09-19 05:16:21 字数 87 浏览 5 评论 0原文

我想在页面离开服务器时更改一些元素文本(page_render、endRequest 等)。

我如何访问该页面以及如何找到元素来更改其值、文本?

I want to change some elements text when page is leaving the server (page_render, endRequest etc.).

How can i get access to the page and how can i find the elements to change their values, texts?

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

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

发布评论

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

评论(3

梦幻的心爱 2024-09-26 05:17:20

有多种选择,您适合哪一种在很大程度上取决于实际目标是什么。

  1. 处理页面的 PreRender 事件并在此事件中调整您想要的任何元素。理想情况下,您可以将其放入由所有需要此处理的页面继承的基类中。这使您可以访问实际的页面模型和控制树。
  2. 设置一个过滤器,使您可以直接访问响应流。您可以通过两种方式实现此目的,作为安装过滤器的单独 HttpModule,或者直接从 Global.asax 安装过滤器。您选择哪种路由取决于您需要的可重用性,其中 HttpModule 的可重用性最高。

这是一篇不错的文章 使用过滤器修改 HTTP 响应

There are a number of options and which one suites you will depend largely on what the actual goal is.

  1. Handle the PreRender event of a Page and adjust any elements you want to in this event. Ideally you would put this in a base class that is inherited by all the pages that require this processing. This gives you access to the actual page model and control tree.
  2. Setup a filter that will give you direct access to the response stream. You can implement this in 2 ways, either as a separate HttpModule that installs the filter or you can install the filter directly from the Global.asax. Which route you choose depends on how reusable you need this, with the HttpModule being the most reusable.

Here is a nice article Modifying the HTTP Response Using Filters

如梦 2024-09-26 05:17:08

除了 HttpModule 之外,您还可以重写“Render”方法(或在基页中执行此操作以使其可重用)。

protected override void Render(HtmlTextWriter writer )
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    base.Render(htmlWriter);

    string html = stringWriter.ToString();

    // do stuff with the html

    writer.Write(html);
} 

Besides HttpModules, you can also override the 'Render' method (or do this in a basepage to make it reusable).

protected override void Render(HtmlTextWriter writer )
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    base.Render(htmlWriter);

    string html = stringWriter.ToString();

    // do stuff with the html

    writer.Write(html);
} 
如日中天 2024-09-26 05:16:57

您可以使用 HttpModule。它位于管道中,可以进行预处理和后处理。

例如,看看这个 whitespaceremover。

You can do so by using a HttpModule. This sits in the pipeline and can do pre- and postprocessing.

For example take a look at this whitespaceremover.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文