I'm trying to scrape my own ASPX page so that I can feed it into HTML Agility Pack parser. I've tried all ways and a string is the only thing I can get to work in this instance.
I'm using the following code to turn an outer control into a string:
static string ConvertControlToString(Control ctl)
{
string s = null;
var sw = new StringWriter();
using (var w = new HtmlTextWriter(sw))
{
ctl.RenderControl(w);
s = sw.ToString();
}
return s;
}
The concept works, except for some annoying glitches. I get the "control must be inside a form with a runat=server" on occasions. It seems to be triggered by controls that cause postback - buttons, update panels etc.
To be clear, my page is in a form, so that's not the issue.
I need to try and work out a solution to my problem, whether that's getting the HTML agility pack parser to work in another way, or to convert the code to a string without errors. It doesn't matter - I just need to get things working.