开发者

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

Can ASCX or ASP.net files be saved as HTML files? 开发者_开发百科if so, how?


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜