开发者

How to get rendered source code of an html page from code behind so I can send it in mail

I created an aspx page wit a button send. on that button click a function code behind goes on. What I want is that when I click on the button to get all the html source code rendered of the aspx page in a variable so I can send it in a mail. how can I get the rendered html source code from code behind fu开发者_如何学JAVAnction.


I actually just wrote a method for this not too long ago. It gets the view at a provided relative path, passes in a provided model, and renders the view as a string.

public static string RenderViewToString(string relativePathToControl, object viewData)
{
    ViewPage viewPage = new ViewPage() { ViewContext = new ViewContext() };

    viewPage.ViewData = new ViewDataDictionary(viewData);
    viewPage.Controls.Add(viewPage.LoadControl(relativePathToControl));

    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            viewPage.RenderControl(tw);
        }
    }

    return sb.ToString();
}

You may need to add a few using statements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜