第三方的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/