获取当前页面的HTML没有ViewState的ASP.Net [英] get HTML of current page without ViewState ASP.Net

查看:115
本文介绍了获取当前页面的HTML没有ViewState的ASP.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法,通过它我可以让我的当前页面的HTML。通过当前的页面我的意思是让我们说我在Default.aspx的工作,并希望通过提供一个按钮来获得HTML

Is there any way through which I can get HTML of my current page. By current page I mean let's say I am working on Default.aspx and want to get HTML by providing a button on it.

如何获得它。

推荐答案

编辑的回应澄清的要求

您可以覆盖网页的渲染方法来捕获在服务器端的HTML源代码。

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
}

这篇关于获取当前页面的HTML没有ViewState的ASP.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆