Thursday, 19 September 2013

Export to Excell stoped working when i added a AJAX panel

Export to Excell stoped working when i added a AJAX panel

I am just starting to learn how to use Ajax and i am stuck on this.
I have buttons in my ASP.net web site that work perfectly but the export
to Excel and Export to CVS stopped working after I added a Ajax panel.
they worked before I added the panel and when I debug the function
To_Excel_Click is called is processed but it just does not create the
excel file. Something in Ajax I think is preventing the file to be created
Thank you for any help
HTML Ajax
</asp:Content>
<asp:Content ID="BodyContent" runat="server"
ContentPlaceHolderID="MainContent">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Conditional">
<contenttemplate>
<asp:Button ID="BT1" runat="server" Text="Submit" Visible="false"
onclick="BT1"/>
<asp:Button ID="BT2" runat="server" Text="Submit" Visible="false"
onclick="BT2" />
<asp:Button ID="BTMakeCVS" runat="server" Text="Submit to CVS"
Visible="false" onclick="BTMakeCVS_Click" />
<asp:Button ID="To_Excel" runat="server" Text="To Excel"
onclick="To_Excel_Click" Visible="false" AutoPostBack="True"/>
</fieldset><fieldset class="Matainancefieldset3">
<asp:GridView ID="GVData" runat="server" Visible="false">
<RowStyle CssClass="Row" />
<AlternatingRowStyle CssClass="AltRow" />
<PagerTemplate><table><tr><td>
<asp:PlaceHolder ID="ph" runat="server">
</asp:PlaceHolder></td></tr></table>
</PagerTemplate>
</asp:GridView>
<asp:Label ID="LBExcell" runat="server" Text="Model:"
Visible="false"></asp:Label>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>
C#
protected void To_Excel_Click(object sender, EventArgs e)
{
ExcelExport.Export("Summary.xls", GVData);
}
public class ExcelExport
{
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition",
string.Format("attachment;filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
table.GridLines = gv.GridLines;
if (gv.HeaderRow != null)
{
ExcelExport.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
foreach (GridViewRow row in gv.Rows)
{
ExcelExport.PrepareControlForExport(row);
table.Rows.Add(row);
}
if (gv.FooterRow != null)
{
ExcelExport.PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
ExcelExport.PrepareControlForExport(current);
}
}
}
}

No comments:

Post a Comment