Serving Word document on button click on C# asp.net page

2.2k Views Asked by At

When code is placed onClick event it does not show open save dialog box and no exception is thrown but works fine onLoad event,opens a open save dialog box to save a word file..

string strDocBody;
strDocBody = "<html " + "xmlns:o='urn:schemas-microsoft-com:office:office' " + "xmlns:w='urn:schemas-microsoft-com:office:word'" + "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head>" + "<title>Version and Release Management</title>";

strDocBody = strDocBody + "<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:Zoom>100</w:Zoom>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->";

strDocBody = strDocBody + "<style> @page" + "{size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt;" + " margin:0.75in 0.50in 0.75in 0.50in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "p.MsoFooter, li.MsoFooter, div.MsoFooter{margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" + "p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}" + "-->" + "</style>" + "</head>";

strDocBody = strDocBody + "<body lang=EN-US style='tab-interval:.5in'>" + "<div class=Section1>" + "<h1>This is my Heading</h1>" + "<h2>This is my Sub Heading</h2>" + "<p style='color:navy;'> This is blue text</p>" + "<p style='font-weight:bold; color:green;'><u> This is green bold underlined text </u></p>" + "<p style='color:red'><I>" + DateTime.Now + "</I></p>" + "<!--[if supportFields]>" + "<div style='mso-element:header' id=h1><p class=MsoHeader><span style='mso-tab-count:4'></span><span style='mso-field-code: PAGE '></span> </p></div>" + "<div style='mso-element:footer' id=f1> " + "<p class=MsoFooter style='border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'><o:p> </o:p></p> " + "Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'>1</span></span> of <span style='mso-field-code: NUMPAGES '></span>" + " <span style='mso-tab-count: 12'> <span style='mso-field-code: DATE '></span> " + " </p></div><![endif]-->" + "</div> </body> </html> ";

//Force this content to be downloaded as a Word document
Response.AddHeader("Content-Type", "application/msword");
Response.AddHeader("Content-disposition", "attachment; filename=TEST.doc");
Response.Charset = "";
Response.Write(strDocBody);
3

There are 3 best solutions below

6
On BEST ANSWER

I suppose you are trying to feed the data in strDocBody as a Word file to the user. In order to do that, this data has to be the only thing that web server passes to the browser. That's why it works fine when you put it in your OnLoad event handler. It's not the case when you put it in button click handler.

If you want this behavior on button click, this button has to redirect user to another URL which will send the document data and nothing else.

Create a page specifically for serving document, let it be Document.aspx. Now, I don't know if you need to perform any additional logic on button click. If not, you can just use LinkButton:

<asp:LinkButton runat="server" ID="docLink" Text="Document" 
                PostBackUrl="Document.aspx" />

Otherwise, just add redirect call in your onClick handler:

protected void btnButton1_Click(object sender, EventArgs e)
{
    // ....
    Response.Redirect("Document.aspx");
}
0
On

based on my experience, this indeed doesn't work for onClick since the headers has already been sent.

0
On

This could be because headers were already posted to the client browser. Try clearing with Response.Clear(), make sure you're not within an Ajax call. Another trick is to open a new page, using a client-side anchor with argument compiled dynamically.

EDIT: of course if you clear your Response, page will be blank after asking the user to download the file. Always keep in mind the stateless synchronous behavior of client-server http communication.