在 ASP.NET 中修改服务器端的 html 输出 [英] Modify html output at server side in ASP.NET

查看:21
本文介绍了在 ASP.NET 中修改服务器端的 html 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第三方的webcontrol生成如下代码来展示自己:

A third-party's webcontrol generates the following code to display itself:

<div id="uwg">
    <input type="checkbox" />
    <div>blah-blah-blah</div>
    <input type="checkbox" />
</div>

可以改成

<div id="uwg">
    <input type="checkbox" disabled checked />
    <div>blah-blah-blah</div>
    <input type="checkbox" disabled checked />
</div>

当我们点击时

<asp:CheckBox id="chk_CheckAll" runat="server" AutoPostBack="true" />

位于同一页面上?

我们需要在服务器端(在 ASP.NET 中)进行.

We need to do it at server side (in ASP.NET).

第三方的控件没有为此提供接口,所以唯一的可能是使用html输出.我应该处理哪个页面事件(如果有)?另外,是否有一些等效于 DOM 模型,或者我需要将输出作为字符串处理?

That third-party's control does not give interface for this, so the only possibility is to work with html output. Which page event should I handle (if any)? Also, is there some equivalent to DOM model, or I need to work with output as string?

推荐答案

当checkbox不在服务端运行或者封装在控件内部时,我们可以使用如下方法:

When checkboxes are not run at server or are encapsulated inside the control, we can use the following method:

protected override void Render(HtmlTextWriter writer)
{
    // setup a TextWriter to capture the markup
    TextWriter tw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(tw);

    // render the markup into our surrogate TextWriter
    base.Render(htw);

    // get the captured markup as a string
    string pageSource = tw.ToString();

    string enabledUnchecked = "<input type="checkbox" />";
    string disabledChecked = "<input type="checkbox" disabled checked />";

    // TODO: need replacing ONLY inside a div with id="uwg"
    string updatedPageSource = pageSource;
    if (chk_CheckAll.Checked)
    {
         updatedPageSource = Regex.Replace(pageSource, enabledUnchecked,
                disabledChecked, RegexOptions.IgnoreCase);
    }

    // render the markup into the output stream verbatim
    writer.Write(updatedPageSource);
}

解决方案来自这里.

这篇关于在 ASP.NET 中修改服务器端的 html 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆