6

I'm having a Visual Webpart that allows user to select Managed Properties and find out what is stored inside it. The result is displayed inside a gridview which he/she can export it to excel.

It is same another export to excel code that I have written:

HttpContext.Current.Response.Clear();
string filename = "Report_" + DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss") + ".xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
HttpContext.Current.Response.ContentType = "application/ms-excel";
GridView tempGridView = new GridView();
tempGridView.AutoGenerateColumns = false;
string strContents = getGridViewHtml(GetExportingGridView(tempGridView));
HttpContext.Current.Response.Write(strContents);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

But after the file is downloaded the page stops responding to any events.

I cannot have one more (dummy) page which can take this request and make a download operation and go off.

Any work around for this issue ?

4 Answers 4

5

I have put your code in the page load event and it resolves the issue.

btnExport.UseSubmitBehavior = false;
btnExport.OnClientClick = "_spFormOnSubmitCalled = false; 
_spSuppressFormOnSubmitWrapper=true;";
1

I had the same issue on my last project. The main problem is sharepoint catches form submits and downloads so you cannot double click a button.

So I added this to the page

<script type="text/javascript">
    _spOriginalFormAction=document.forms[0].action;
    _spSurppressFormOnSubmitWrapper = true;
</script>

I Think that fixed that issue, but I ended up using the SharePoint download page:

/_layouts/download.aspx?sourceurl=[file url]&source=[current page]

3
  • Hi Thantos, thanks for the quick reply. So do you mean that merely pasting that script will not work ? I need to use the download.aspx page as well ? If yes then, I didn't understand what do you mean by [file url] as my code is just exporting the gridview content to excelsheet. Also [current page] ? you mean the just "audit.aspx" or the entire URL ? thanks.
    – Amin Sayed
    Sep 13, 2012 at 14:01
  • Just try the code, if you are just exporting the data directly, the download page won't work for you, only if it was a sp library doc, so try the script. Sep 13, 2012 at 14:38
  • Thantos. Thanks for the reply.. just figured out the solution for this. You may check and comment. Thanks again.
    – Amin Sayed
    Sep 14, 2012 at 12:46
1

Ok I just figured it out.

We are just making the export button not to act like a form submit button.

Below is the code for this.

  btnExport.UseSubmitBehavior = false;
  btnExport.OnClientClick = "_spFormOnSubmitCalled = false; _spSuppressFormOnSubmitWrapper=true;";
1
  • Of course not having submit the form always helps. Good find. Sep 14, 2012 at 15:05
1

The following link was very helpful in understanding the response issue that I was facing. It was solved by setting a few flags.

http://mytechsoup.com/?p=36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.