Change server side html output in ASP.NET

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

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

Is it possible to change it to

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

When we push

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

is on the same page?

We need to do this on the server side (in ASP.NET).

This third-party control does not provide an interface for this, so the only option is to work with html output. What page event should be handled (if any)? Also, is there any equivalent to the DOM model, or do I need to work with the output as a string?

+6
html event-handling rendering
source share
2 answers

When the checkboxes do not run on the 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); } 

The solution is taken from here .

+20
source share

Inherit it and find the controls in the control tree and set the appropriate attributes.

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

Obviously, this is fragile if the control changes its output depending on other properties or updates the library; but if you need a workaround this will work.

+4
source share

All Articles