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.
请告诉我如何在按钮点击时将当前页面保存为html页面。我的页面仅包含我在页面加载事件中填写的标签。
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).
我正在使用下面的代码,但它没有保存(在HTML中)我加载页面时看到的所有值(我认为它在页面上加载值之前转换)。
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!
请帮帮我!
2 个解决方案
#1
4
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将aspx的内容写入html字符串。
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.
那么基本上你只需要将它写入一个字符串文件并将其保存为HTML扩展。
System.IO.File.WriteAllText(@"C:\yoursite.htm", s);
#2
2
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
我知道这个问题发布已经有6年了。但是,我在这里得到了一个很好的参考: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)
}
#1
4
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将aspx的内容写入html字符串。
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.
那么基本上你只需要将它写入一个字符串文件并将其保存为HTML扩展。
System.IO.File.WriteAllText(@"C:\yoursite.htm", s);
#2
2
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
我知道这个问题发布已经有6年了。但是,我在这里得到了一个很好的参考: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)
}