Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save current aspx page as html

Tags:

html

c#

asp.net

Please tell me how to save current page as a html page on button click. My page contains only labels which I fill on page load event.

I am using the code below for this, but it's not saving (in HTML) all of the values that I see when my page is loaded (I think it converts before the values get loaded on the page).

private void saveCurrentAspxToHTML()
{
    string HTMLfile = "http://localhost:4997/MEA5/AEPRINT.aspx?id=" +
                      Convert.ToString(frmae.AeEventid) +
                      "&eid=" +
                      Convert.ToString(frmae.AeEnquiryid);

    WebRequest myRequest = WebRequest.Create(HTMLfile);

    // Return the response.
    WebResponse myResponse = myRequest.GetResponse();

    // Obtain a 'Stream' object associated with the response object.
    Stream ReceiveStream = myResponse.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

    // Pipe the stream to a higher level stream reader with the required encoding format. 
    StreamReader readStream = new StreamReader(ReceiveStream, encode);

    // Read 256 charcters at a time.
    Char[] read = new Char[256];
    int count = readStream.Read(read, 0, 256);

    using (StreamWriter sw = new StreamWriter(Server.MapPath("~") + "\\MyPage.htm"))
    {
        while (count > 0)
        {
            // Dump the 256 characters on a string and display the string onto the console.
            String str = new String(read, 0, count);
            sw.Write(str);
            count = readStream.Read(read, 0, 256);
        }
    }

    // Close the response to free resources.
    myResponse.Close();

}

Please help me!

like image 491
Dr. Rajesh Rolen Avatar asked Jul 17 '10 05:07

Dr. Rajesh Rolen


People also ask

How link ASPX page to html?

1. Create a new web form in visual web developer. 2. Copy the content from body of the html file , paste it in body of the aspx page.

Is ASPX same as html?

HTML allows web browsers to interpret display content written between tags. It allows images and objects to be embedded in the webpage. ASP is used to design user-interactive or dynamic web pages. HTML is basically used to create static web pages.

How can I run ASPX file in web browser?

You can use Firefox, Chrome, Edge, or any browser. All you have to do is, right-click on the . aspx file, click on Open with, and select Chrome (your browser). If you can't find your desired browser, click on Choose another app and locate your specified browser from the Program file.

How do I download a PDF from ASPX?

To do this, press "Ctrl + P" to open the print settings of the web page. From the pop-up window, press the "Change" button under the "Destination" tab to convert the document into a PDF file. You will now have to select the "Save as PDF" option.


2 Answers

I'm a little sketchy on the actual code. But a while ago I did something similar. I used a StringWriter to write the content of the aspx to html string.

  StringWriter sw = new StringWriter();
  HtmlTextWriter w = new HtmlTextWriter(sw);
  divForm.RenderControl(w);
  string s = sw.GetStringBuilder().ToString();

Then basically you just have to write that to a string file and save it as an HTML extension.

System.IO.File.WriteAllText(@"C:\yoursite.htm", s);
like image 112
Dewald Henning Avatar answered Oct 02 '22 15:10

Dewald Henning


I know it's already 6 years since the question was posted. But, I got a good reference here: https://weblog.west-wind.com/posts/2004/Jun/08/Capturing-Output-from-ASPNet-Pages

Perhaps it will be useful for the other reader.

protected override void Render(HtmlTextWriter writer)
{
        // *** Write the HTML into this string builder
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
        base.Render(hWriter);

        // *** store to a string
        string PageResult = sb.ToString(); //PageResult contains the HTML

        // *** Write it back to the server
        writer.Write(PageResult)
}
like image 35
Yusril Maulidan Raji Avatar answered Oct 02 '22 15:10

Yusril Maulidan Raji