2

I am able to get the HTML from the code-behind, like this one:

protected override void OnPreRenderComplete(EventArgs e)
{
    StringWriter sw = new StringWriter();
    base.Render(new HtmlTextWriter(sw));
    sbHtml = sw.GetStringBuilder();
    Response.Write(sbHtml + "<!-- processed by code-behind -->");
}

But I need to remove the HTML from the Page, any help?

6
  • 2
    Could you be a bit more specific with what you want to do? Commented Jul 8, 2011 at 14:52
  • 6
    This is an odd question. You clearly have access to the page itself, so why not just write the page differently (i.e. add/change/remove text from the .aspx) rather than messing with OnPreRenderComplete(). Can you tell us what you're trying to achieve? Commented Jul 8, 2011 at 14:53
  • are you using server controls or do you want to compose and render all the page simply by kicking out raw HTML like in your example above using StringWriter? Commented Jul 8, 2011 at 14:54
  • 1
    To remove all the html from the page you can use Response.Clear(); But the best solution would to remove the controls you don't want to render from the page. Commented Jul 8, 2011 at 14:58
  • More specific: Integrating with another API, which needs to parse the HTML and returns final one. This integration needs to be done only on certain situation, other situations I have leave it as such. Commented Jul 8, 2011 at 16:28

3 Answers 3

3

If I understand well you wish to manipulate the sbHtml, and write it out.

sbHtml = sw.GetStringBuilder();

sbHtml.Replace('anything','to anything');

Response.Write(sbHtml);

(or is something else ?)

Sign up to request clarification or add additional context in comments.

Comments

2

Did you want a method like this to strip the HTML?

public static string StripHTML(string HTMLText)
{
    var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
    return reg.Replace(HTMLText, "").Replace("&nbsp;", "");
}

Comments

0

You can put an <asp:placeholder> on the page and set the contents to whatever you want. Add/remove/whatever.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.