asp.net - 在 ASP.NET 中修改服务器端的 html 输出

标签 asp.net html event-handling rendering

第三方的webcontrol生成如下代码显示自身:

<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 中)完成。

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

最佳答案

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

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);
}

解决方案取自here .

关于asp.net - 在 ASP.NET 中修改服务器端的 html 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/883922/

相关文章:

jquery - 我可以使用 jQuery 在准备好的文档上模拟单击吗?

输入类型 ="file"上的 jQuery 更改方法

jquery - 切换 jQuery 事件状态(开/关)而无需再次指定处理程序

javascript - 编辑模式下页面的共享点自定义样式

c# - 如何将文件放入 HttpResponseMessage 内容?

javascript - 在子文件夹中运行的 Angular 7 中的路由无法正常工作

javascript - 从按钮调用 javascript 函数会在我的 ASP.net 中隐藏 FullCalendar

javascript - html/javascript 'widget' 如何知道用户何时单击了小部件之外的任何位置?

html - 移动 web 应用程序的 css 放置图标

javascript - 在移动设备旋转时重新加载 javascript?