tags:

views:

96

answers:

3

I want parse the html of current page. How can I get the html of current page for that in asp.net?

Thanks in advance.

+2  A: 

Override Render method and call base.Render with you own HtmlWriter.

Alex Reitbort
Is there a way to get html of current page using Request.Url?
Constantine
What do mean "current page using Request.Url"????
Alex Reitbort
+2  A: 

for client side

In Internet explorer

Right click on the browser --> View source

IN firefox

Right click on the browser --> View Page Source

for server side

You can override the page's render method to capture the HTML source on the server-side.

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();

    // render the markup into the output stream verbatim
    writer.Write(pageSource);

    // remove the viewstate field from the captured markup
    string viewStateRemoved = Regex.Replace(pageSource,
        "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
        "", RegexOptions.IgnoreCase);

    // the page source, without the viewstate field, is in viewStateRemoved
    // do what you like with it
}
solairaja
you missed server-client - HttpRequest and HttpResponse
ck
included in the second run :)
solairaja
+1  A: 

Do you really want to parse HTML? It's a tricky business. If you don't absolutely have to do it, I'd avoid it by using DOM methods client-side (if a client-side solution is acceptable). If you're doing a lot of it, you might consider jQuery, Prototype, or some other tool to help.

T.J. Crowder