How to make page active after download using Response in C# in sharepoint webpart/user control
I usually came across these kind of statements 'Page hangs after downloading', 'post back not happening after file download'
yes, its true, when we are trying to download a file from C# and with the logic of Response, the last statement of response will be response.end.
once the compiler hits this response.end, the page losts its response and hence, any button or post back controls wont work. It looks like the page and controls are numb.
Think 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 javascript function like shown below.
I usually came across these kind of statements 'Page hangs after downloading', 'post back not happening after file download'
yes, its true, when we are trying to download a file from C# and with the logic of Response, the last statement of response will be response.end.
once the compiler hits this response.end, the page losts its response and hence, any button or post back controls wont work. It looks like the page and controls are numb.
Think 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 javascript function like shown below.
<asp:Button ID="generateExcel" runat="server" Text="Export Excel"
OnClick="generateExcel_Click" CssClass="rptSubmitButton"
OnClientClick="javascript:setFormSubmitToFalse()" />
Then in javascript function write as below.
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.
No comments:
Post a Comment