Thursday, 6 February 2014

No more post back after file download in sharepoint

I had this issue with sharepoint. I have a button on the page that sends a file and after clicking the button, the rest of the form was unresponsive. Turns out it is a sharepoint thing that sets the variable _spFormOnSubmitCalled to true to prevent any further submits. When we send a file this doesn't refresh the page so we need to manually set this variable back to false.
On your button in the webpart set the OnClientClick to a function in your javascript for the page.

 <asp:Button ID="generateExcel" runat="server" Text="Export Excel" 
OnClick="generateExcel_Click" CssClass="rptSubmitButton"
OnClientClick="javascript:setFormSubmitToFalse()" />
Then in the javascript I have this function.
function setFormSubmitToFalse() {
    setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
    return true;
The 3 second pause I found was necessary because otherwise I was setting the variable before sharepoint set it. This way I let sharepoint set it normally then I set it back to false right after.

in C#

SharePoint registers a JavaScript "on submit" handler. In this handler the global variable _spFormOnSubmitCalled is set to true. SharePoint uses this variable to check if a submit was executed and prevents any further submits. Since your "download postback" does not refresh the page this variable remains true. With the effect that that all other buttons stop working.
As a workaround you can set this variable to false in a client click handler on your download button:

Button btn = new Button();
btn.Text = "Download";
btn.Click += DownloadButton_Click;

// set the client click handler
btn.OnClientClick = "window.setTimeout(function() { _spFormOnSubmitCalled = false; }, 10);"

No comments:

Post a Comment