Friday, July 3, 2015

Postback problem after file download

There is a strange problem in Sharepoint pages when dynamically creating and downloading files. After downloading a file page controls lose the ability of making postback. The problem occurs because of a variable named _spFormOnSubmitCalled, which must have value "false" to be able to make postback. This variable has been implemented to prevent multiple submits. So, when downloading a file, the page isn't being fully refreshed, so this variable value changes to "true".
The workaround is to manually set this property value to "false"

This answer provides solution for this situation.

The solution is simple. Just register client side onclick event to the button and change the property value by javascript:

<asp:Button ID="btnDownloadFile" runat="server" Text="Download file" 
OnClick="btnDownloadFile_Click"
OnClientClick="javascript:setFormSubmitToFalse()" />

Setting timeout value 3 seconds is important to be sure Sharepoint has already changed the property value and we are changing it back:

function setFormSubmitToFalse() {
    setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
    return true;
}

No comments:

Post a Comment