Can ASCX or ASP.net files be saved as HTML files

独自空忆成欢 提交于 2019-12-12 02:39:55

问题


Can ASCX or ASP.net files be saved as HTML files? if so, how?


回答1:


yes, it is possible, to get the rendered content of a User Control, do the following :

StringWriter output = new StringWriter();
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl("path to ascx file");
pageHolder.Controls.Add(viewControl);
HttpContext.Current.Server.Execute(pageHolder, output, true);
string htmlOutput = output.ToString();

Am sure you can adapt the above for ASPX page if required :-)

From there, saving that to a file should be fairly straight forward.

HTH. D




回答2:


You can do this directly with the Render method of a Page or UserControl. Since the method is protected, you will need to create a control that subclasses either. From there, you've got access to do whatever you need.

e.g.

public partial class MyPage: Page
{
    public string GetPageContents()
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        using (HtmlTextWriter writer = new HtmlTextWriter(sw))
        {
              Render(writer);
        }
        return sb.ToString();
    }
}

You probably wouldn't want to call this anytime before the PreRenderComplete event of the page though, since otherwise you can't be sure all child controls/events/etc have finished.



来源:https://stackoverflow.com/questions/6271054/can-ascx-or-asp-net-files-be-saved-as-html-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!