1

This linkbutton stops working after I do a postback. The JS will still run and will correctly return true/false. The postback that breaks this is exporting a data table to Excel. Note: this code works as expected outside of SharePoint.

<asp:LinkButton ID="btnGenerateReport" runat="server" 
    CssClass="generateReport button wide"
    OnClick="btnGenerateReport_Click" 
    OnClientClick="if (!ValidateMe()) return false;" 
    UseSubmitBehavior="true" CausesValidation="false">
    Generate Report
</asp:LinkButton> 


System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(memoryStream);
            CsvHelper.CsvWriter writer = new CsvHelper.CsvWriter(streamWriter);
            foreach (DataColumn column in tableToExport.Columns)
            {
                writer.WriteField(column.ColumnName);
            }
            writer.NextRecord();

            foreach (DataRow row in tableToExport.Rows)
            {
                for (var i = 0; i < tableToExport.Columns.Count; i++)
                {
                    writer.WriteField(row[i]);
                }
                writer.NextRecord();
            }

            streamWriter.Flush();

            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
            HttpContext.Current.Response.End();
  • Post the excel generation code. I think the issue lies with the way you are handling Response object. – Amal Hashim Jul 19 '16 at 17:47
  • Sorry, I read this as *potluck. They held an office get-together and stuff broke. – Jammin4CO Jul 19 '16 at 18:09
  • 2
    found the answer here: stackoverflow.com/questions/2336883/… – tomsmithweb Jul 19 '16 at 18:18
  • The answer is saying it is SharePoint related. – tomsmithweb Jul 19 '16 at 18:20
  • 1
    Can confirm that. The spFormOnSubmitCalled indeed blocks multiple postbacks on the same page. See my answer here for some workarounds too if you still need it - I could also copy the relevant content here too if you like. Anyway, voting to reopen this. As a suggestion, you could add some pointer in the question to indicate that the issue is SharePoint relate, for example you could add a notice that say that the code works as expected outside of SharePoint. – SPArchaeologist-様 Jul 20 '16 at 17:50
0

This occurs because of Sharepoint sets the _spFormOnSubmitCalled to true and disallow more submits unless it is set back to false as occured in postback behavior.

So at OnClientClick event you should set _spFormOnSubmitCalled to false;

The final code should look like

<asp:LinkButton ID="btnGenerateReport" runat="server" 
    CssClass="generateReport button wide"
    OnClick="btnGenerateReport_Click" 
    OnClientClick="javascript:setFormSubmitToFalse()" 
    UseSubmitBehavior="true" CausesValidation="false">
    Generate Report
</asp:LinkButton> 

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.