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 “andyspearsposted 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

5 Comments »

  1. JC Said,

    July 17, 2008@ 1:24 pm      Reply

    Thanks for posting this. It saved me what probably would have been hours. 🙂

  2. Carlos Said,

    July 17, 2008@ 8:43 pm      Reply

    Thanks for posting this, but i have a scriptmanager for ajax and this script fail. Do you know how can i slove it?

  3. laflour Said,

    July 17, 2008@ 9:19 pm      Reply

    Do you have SP1 installed?
    Did you change the web.config to add all AJAX specific stuff, like httphandlers?

    If everything done and and you put ScriptManager, then what exception do u have?

  4. Joe Said,

    August 16, 2010@ 9:47 am      Reply

    More Than good,

    very Thank
    Joe,

  5. Mike Said,

    October 12, 2010@ 4:10 pm      Reply

    It”s not working with sharepoint 2010.
    Any idea?

    Mike


RSS feed for comments on this post · TrackBack URI

Leave a Comment