Skip to content Skip to sidebar Skip to footer

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?

Solution 1:

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

StringWriteroutput=newStringWriter();
PagepageHolder=newPage();
UserControlviewControl= (UserControl)pageHolder.LoadControl("path to ascx file");
pageHolder.Controls.Add(viewControl);
HttpContext.Current.Server.Execute(pageHolder, output, true);
stringhtmlOutput= 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

Solution 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.

publicpartialclassMyPage: Page
{
    publicstringGetPageContents()
    {
        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.

Post a Comment for "Can Ascx Or Asp.net Files Be Saved As Html Files"