Question:
This is how we are handling exporting to excel. I works fine when not surrounded by an Update Panel, but if I surround it with an Update Panel I get the ever so informing Unknown Error and in the output it throws an exception:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dllAn exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
Here is My code that works not using the Update Panel 1 #region ExcelImageButton_Click
2 protected void ExcelImageButton_Click(object sender, EventArgs e)
3 {
4 // Serialize DataSource
5 XmlDocument input = new XmlDocument();
6 input = SerializeProductionVariance(dataSource as IList);
7
8 Response.Clear();
9 Response.ContentType = "application/vnd.ms-excel";
10 Response.Charset = "";
11 if (Selection.Count > 1)
12 {
13 Response.AddHeader("content-disposition", "attachment; filename=MultiSelect.xls");
14 }
15 else
16 {
17 Response.AddHeader("content-disposition", "attachment; filename=Selection[0].Name + ".xls");
18 }
19
20 XsltArgumentList args = new XsltArgumentList();
21 args.AddParam("date1", "", ((DateTime)StartWebDateChooser.Value).ToShortDateString());
22 args.AddParam("date2", "", ((DateTime)EndWebDateChooser.Value).ToShortDateString());
23 XslCompiledTransform xslt = new XslCompiledTransform();
24 xslt.Load(Server.MapPath("~/Resources/Xsl/ExcelExportPM.xsl"));
25 xslt.Transform(input, args, Response.OutputStream);
26 Response.End();
27 }
28 #endregion
I get the error on the Response.End();
And only when I surrond the button in an UpdatePanel. But I need to surrond the whole user control this button is in. Any suggestions
Answer1:
Hi
Make sure that your export button is out side of the update panel.
Answer2:
And that's my problem. The usercontrol is surrounded by an UpdatePanel and has to be surrounded by said UpdatePanel.
Answer3:
hello.
tell me: when you click that button, do you only generate the excell file? if so, you can try a small workaround...instead of putting the code on a button click, put it in a custom handler. then, change the button so that it's a simple html button. this button then could try one of the following things:
1. open a new window which points to the handler
2. create an iframe and point it to the handler
Answer4:
I've been facing this problem as well, my work mate just showed me that, in the UpdatePanel definition, presumably you have a Triggers collection (for timers, etc) You can also add a <asp:PostBackTrigger> item that has an attribute controlID set that to the button to generate the Excel sheet (pretty much exactly what i've been doing) and it immediately started working.
e.g.
<asp:UpdatePanel runat="server" ID="updatepanel3" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger EventName="Tick" ControlID="timerAuction" />
<asp:PostBackTrigger ControlID="btnGenerateSheet1" />
<asp:PostBackTrigger ControlID="btnGenerateSheet2" />
</Triggers>
<ContentTemplate>
Answer5:
Hey...
Thanks a ton..I spent eons of time trying to figure out what was wrong with my code. I added the <asp:PostBackTrigger> after looking at this post and it worked like charm...Thanks again!!!