Reputation: 2479
I'm trying to provide an option to download a report (in csv format), allowing the user to select a specific date range. (I also have an option to display the same information in a GridView on the page). I can do this just fine by using Response.Clear(), setting the ContentType and content-disposition, using Response.Write(), and then Response.End(), like this blog, and many others.
However, after doing this, the buttons no longer seem to be posting back to the server. I can update my report in the GridView however many times I like, but as soon as I export neither button will respond (i.e. no further server side events are generated). How can I get a file and keep the buttons respoding? This is on a SharePoint application page, so there's a bunch of stuff going on in a Master page.
Upvotes: 0
Views: 2335
Reputation: 161
I had a similar issue. Had a form with a grid and a button that was supposed to export the grid to an excel sheet.
Using this piece of javascript within the markup fixed it for me:
<script type='text/javascript'>
_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper = true;
</script>
Upvotes: 1
Reputation: 2479
And I found an answer in this question
SharePoint registers a variable called "_spFormOnSubmitCalled" which will prevent any postbacks once it is set to true. I've added an OnClientClick to my button to set it back to false after a short delay, as in Stefan's answer.
Upvotes: 0
Reputation: 2570
asp code :
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" Font-Names = "Arial" Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" HeaderStyle BackColor = "green" AllowPaging ="true" OnPageIndexChanging = "OnPaging" >
<Columns>
<asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID" HeaderText ="CustomerID" />
<asp:BoundField ItemStyle-Width = "150px" DataField = "City" HeaderText = "City"/
<asp:BoundField ItemStyle-Width = "150px" DataField = "Country" HeaderText = "Country"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode" HeaderText = "PostalCode"/>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="ExportToCSV" OnClick="btnExportCSV_Click" />
</form>
Protected void btnExportCSV_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
Upvotes: 0