OK, I am most likely just missing something basic but I am super frustrated by this.
public partial class myClass: System.Web.UI.Page
{
public String pageHtml{
get
{
if (ViewState["pageHtml"] != null)
{
return (string)ViewState["pageHtml"];
}
else
{
return null;
}
}
set
{
ViewState["pageHtml"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sbOut = new StringBuilder();
StringWriter swOut = new StringWriter(sbOut);
HtmlTextWriter htwOut = new HtmlTextWriter(swOut);
base.Render(htwOut);
string sOut = sbOut.ToString();
pageHtml = sOut;
writer.Write(sOut);
}
protected void btnDownloadPage_Click(object sender, EventArgs e)
{
DownloadTextAsFile("test", pageHtml, "aspx");
}
public void DownloadTextAsFile(string filename, string content, string extention)
{
string downloadFileName = filename + "." + extention;
Response.AddHeader("Content-disposition", "attachment; filename=" + downloadFileName);
Response.ContentType = "application/octet-stream";
Response.Write(content);
Response.End();
}
}
when the DownloadPage button is clicked pageHtml is always null. What gives? I have tried moving the “pageHtml = sOut;” To after the “writer.Write(sOut);” And I have tried making pageHtml not persist to the viewstate but no such luck. (also I am aware my pageHtml get is not really checking anything as it returns null if ViewState[“pageHtml”] is null and I could just return ViewState[“pageHtml”] without the if-else check but I don’t care about that right now)
I think the ViewState doesn’t survive after a postback (your button click).
Try using session state instead.