修改ASP.NET中服务器端的html输出

Rom*_*oma 7 html asp.net rendering event-handling

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

<div id="uwg">
    <input type="checkbox" />
    <div>blah-blah-blah</div>
    <input type="checkbox" />
</div>
Run Code Online (Sandbox Code Playgroud)

是否可以将其更改为

<div id="uwg">
    <input type="checkbox" disabled checked />
    <div>blah-blah-blah</div>
    <input type="checkbox" disabled checked />
</div>
Run Code Online (Sandbox Code Playgroud)

当我们点击

<asp:CheckBox id="chk_CheckAll" runat="server" AutoPostBack="true" />
Run Code Online (Sandbox Code Playgroud)

位于同一页面?

我们需要在服务器端(在ASP.NET中)执行此操作.

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

Rom*_*oma 21

当复选框未在服务器上运行或封装在控件内时,我们可以使用以下方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

解决方案来自这里.


Tom*_*ter 5

继承它并在控件树中找到控件,然后适当设置属性。

 protected override void OnPreRender(EventArgs e)
 {
      base.OnPreRender(e);
      (this.Controls[6] as CheckBox).Disabled = true;
 }
Run Code Online (Sandbox Code Playgroud)

显然,如果控件将根据其他属性修改其输出,或者升级库,则这是脆弱的。但是如果您需要解决方法,则可以使用。