Modify html output at server side in ASP.NET(在 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 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ASP.NET 中修改服务器端的 html 输出


- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01