none
After file added to response, post back stops working in WebPart

    Question

  • I have a webpart (2 actually) that use SPMenu items to allows users to download files (pdf, word, tiff, could be anything).  The SPMenu is working fine and so does the file download.  The problem is that once a file has been added to the page response and the page is recieved by the browser, all controls loose the ability to post back.  For instance, if I press a button that is supposed to do a post back nothing happens.  I have run through the javas

    Here is a simplified version of my code.
     
    Code Snippet

    public void AddFileFromUrlToResponse(string url, string fileName, bool endRequest
     {
    byte[] bytes;
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        try
        {
            bytes = client.DownloadData(url);
        }
        catch (Exception ex)
        {
            throw new Exception(String.Format("Unable to download file from url {0}.", url), ex);
        }
    }

    Context.Response.AddHeader("Content-Disposition",
        String.Format("attachment; filename={0}", fileName));
    Context.Response.AddHeader("Content-Length", bytes.Length.ToString());
    Context.Response.ContentType = "application/octet-stream";

    if (bytes.Length > 0)
    {
        Context.Response.BinaryWrite(bytes);
    }
    else
    {
        log.Warn(String.Format("File from url {0} was zero bytes in length. Cannot download.", url));
    }

    Context.Response.Flush();

    if (endRequest)
    {
        Context.Response.End();
    }
    }

    The WebClient section works fine.  I have tried playing with different ContentTypes, ending the response, flushing the response, setting the Content-Disposition to inline, but none of it made a difference.  If I remove the Content-Disposition header and the BinaryWrite lines then the problem goes away but then I then the file is missing.  I have looked at several examples on this forum and elsewhere and it appears that I am doing everything properly  Any ideas?  Thanks.

    - Tyson  
    Wednesday, April 30, 2008 12:33 AM

Answers

All replies

  • I suspect your problem is with the SharePoint javascript, which prevent more than one postback on a page. Essentially core.js (or init.js??) contains some javascript which prevents the second postback.

     

    This post exaplains where ... http://blog.thekid.me.uk/archive/2007/01/19/using-asp-net-ajax-with-sharepoint-moss-2007.aspx

     

    The post releates to AJAX, as the SP javascript prevented AJAX working, but I suspect also applies to your problem.

     

    SP1 fixed the AJAX problem and may fix yours?

     

    --Vince

     

    Thursday, May 01, 2008 10:34 PM
  • Hi there ...

    Sorry to intrude but I think I have an almost identical problem, but I don't quite understand the idea of SharePoint Javascript, can somebody help me out?

    I have a page from which the user can generate a Word document, which is posted back to the user as a MIME attachment. That's not the problem as such, as it works with Firefox on the XP machine that I'm developing on. However, on a user's XP machine, after the attachment is opened automatically in Word, all the buttons on the page that use javascript:__doPostBack no longer work, as though the event isn't generated. So it seems to make sense that it is a similiar problem, i.e., the subsequent postback is not being accepted ...

    I can't recreate the problem on my XP machine, and I can't spot any significant difference in the configuration of Firefox either, so I assume it must be some client-side configuration issue, but does anybody have an idea of what I'm missing? How can I check if there are different versions of Javascript on the machines? And if the versions are different, how can I rectify the issue on the users machine? Please help if you can, as I am being shouted at!!

    Regards


    Tuesday, May 13, 2008 3:41 PM
  • I ran into this exact problem and after several hours of digging around in SharePoint's code, TheKid is right.  Every time the form is submitted, the function WebForm_OnSubmit is called.  From there the function _spFormOnSubmitWrapper is called (located in the INIT.JS file).  In that function, it checks to see if a variable "_spFormOnSubmit" has been set to true, and if it has, return false which cancels the submit request!  I think it's there to prevent users from accidentially posting back twice during the same page sequence.  Anyways, here is how I worked around this problem:

     

    1) Set your button's client-side click event to: "exportRequested=true;"
    2) register the following JS:

    Code Snippet

    string beforeSubmitJS = "\nvar exportRequested = false; \n";
    beforeSubmitJS += "var beforeFormSubmitFunction = theForm.onsubmit; \n";
    beforeSubmitJS += "theForm.onsubmit = function(){ \n";
    beforeSubmitJS += "var returnVal = beforeFormSubmitFunction(); \n";
    beforeSubmitJS += "if(exportRequested && returnVal){_spFormOnSubmitCalled=false; exportRequested=false;} \n";
    beforeSubmitJS += "return returnVal; \n";
    beforeSubmitJS += "}; \n";
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS, true);

     

     

    I haven't fully tested this code out, but it seems to do the trick.  Hope this helps anyone that runs into the same problem!

    • Proposed as answer by Tony Yin Wednesday, July 22, 2009 4:43 PM
    Thursday, June 05, 2008 12:59 PM
  • Thnaks dude, Its really helped me a lot. Me to stuck with the same problem. Many many thanks.

    Regards,

    Bala.

    Tuesday, July 06, 2010 5:48 AM
  • SharePoint registers a JavaScript "on submit" handler through INIT.js. 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 "Download postback" does not refresh the page this variable remains true.

    Hence subsequent button clicks stop working. There is an easy way to work around this issue. Register OnClientClick event for the button click and set _spFormOnSubmitCalled to false.

    <asp:Button ID="Button1" runat="server" Text="Export" onclick="Button1_Click" OnClientClick="javascript:setFormSubmitToFalse()" /> 


    And put the script below in the page/js file,

    <script type="text/javascript"> function setFormSubmitToFalse() { _spFormOnSubmitCalled = false; return true; } </script> 

     

    • Edited by ShivaSK Sunday, November 06, 2011 12:28 PM formatting
    Sunday, November 06, 2011 12:27 PM
  • @Tony Yin, this works well except I had to register the script using:

    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alterFormSubmitEvent", beforeSubmitJS);

    rather than RegisterStartupScript, because theForm.onsubmit was not yet defined at that time (was null).

    Remember to add the requisite <script type=\"text/javascript\"> at the start and </script> at the end.

    Monday, February 25, 2013 6:26 PM