Page events stop working after using output stream for binary download
In my previous SharePoint projects I've met the situations when you need to outstreem some binary files, like PDF file or something else, but when you do this by the standard way (how you used to do in normal ASP.NET site) you found that page is "dead" after you streamed you file to the client. By "dead" I mean that it doesn't respond to any events, and you can't use this page.
The problem lies in the SharePoint JavaScript files, which are integrated into the each web page, and seems that they protect you from unnecessary double-posts. But unfortunately such behaviour breaks your stream output.
I didn't have a change to investigate this problem in details, to find out what exactly affects on this, and always solved this by putting stream out in the new popup window.
In these days on SharePoint NGs this question was risen and "andyspears" posted a work-around, how to change the SharePoint JS and return the normal behaviour back.
1: //1) Set your button's client-side click event to:
2: "exportRequested=true;"
3:
4: //2) register the following JS:
5: string beforeSubmitJS = "\nvar exportRequested = false; \n";
6: beforeSubmitJS += "var beforeFormSubmitFunction = theForm.onsubmit; \n";
7: beforeSubmitJS += "theForm.onsubmit = function(){ \n"; 8: beforeSubmitJS += "var returnVal = beforeFormSubmitFunction(); \n";
9: beforeSubmitJS += "if(exportRequested && returnVal)
10: {_spFormOnSubmitCalled=false; exportRequested=false;} \n"; 11:
12: //beforeSubmitJS += "alert(returnVal + '\\n' + _spFormOnSubmitCalled);";
13: beforeSubmitJS += "return returnVal; \n";
14: beforeSubmitJS += "}; \n";
15:
16: //beforeSubmitJS += "alert(theForm.onsubmit);";
17: this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true);
Thanks AdnySpears, good work!
Mirror: Page events stop working after using output stream for binary download