Showing posts with label exporting. Show all posts
Showing posts with label exporting. Show all posts

Monday, March 26, 2012

Exporting GridView Values to Excel...

Hey... i get this error when i am exporting a gridview to a excel file. I know that is something with AJAX...

sys.webForm.PageRequstManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are the response is modified by calls to response.write(), response filters, http modules or server trace is enable.

Tongue Tied

You can't use the Response object to write a file directly back on a partial postback's response. That's the source of your error.

However, you canuse an iframe to simulate an AJAX file download.


hello.

just to add a link to my post about that subject:

http://msmvps.com/blogs/luisabreu/archive/2007/10/17/asp-net-ajax-some-ideas-on-how-to-export-the-contents-of-a-gridview-to-an-excel-file-without-a-full-postback.aspx

Exporting gridview to excel and Ajax page get an error

hello I trying to export a gridview to excel in my project, but I got this error:

"Sys.WebForms.PageRequestManagerParserErrorException:The message received from the server could not be parsed. Common causesfor this error are when the response is modified by calls toResponse.Write(), response filters, HttpModules, or server trace isenabled."

is there a way to export this without response.write or to solve this issue with ajax

I solve the problem like this:

protectedvoid Page_Init()

{

PostBackTrigger trigger =newPostBackTrigger();

trigger.ControlID = ((Button)pnGridViewProcess.FindControl("btnExcel")).UniqueID.ToString();

((System.Web.UI.UpdatePanel)Page.Controls[0].FindControl("UpdatePanel1")).Triggers.Add(trigger);

}

exporting gridview to excel

hi

friends

i want to export gridview data to the excel in an ajax update panel

can any one of u please help regarding it it is working fine in normal website

it is not working in a ajaxenabledwebsite

What method are you using to export the data to excel? I recommend to use Aspose.Cells.


Hi,

I am afraid that this is normol.

You get the following error,don't you?

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled

I think the error message explains the issue clearly.

You can set EnablePartialRendering=false for ScriptManager. then it works fine.

For more inormation, see:

Export GridView to Excel Complete Code!

Exporting gridview to excel and Ajax page get an error

Best Regards,


Hello.

you cannot integrate that technique easily on a page that has an updatepanel. anyways, i've written apost that exposes some ideas on how youmight get a solution for this kind of problem:

http://msmvps.com/blogs/luisabreu/archive/2007/10/17/asp-net-ajax-some-ideas-on-how-to-export-the-contents-of-a-gridview-to-an-excel-file-without-a-full-postback.aspx

Exporting GridView Contents to Excel Spreadsheet

Hello,

I currently have a report that takes about 3 minutes to process before outputting around 1500 lines to a non-paginated gridview. I wanted to add an UpdateProgress with an animated GIF to let the user know that it is processing, and add a cancel button using the new functionality from AJAX RC.

I cannot completely successfully implement my idea. If I leave the gridview in the UpdatePanel, it will update correctly and display on the page, but when I click the Export to Excel button, it causes some Response.Writes to happen, and it throws a Sys.Webforms.parsererror (which I have looked at some discussions of). The second option is to leave the gridview out of the update panel, which allows the data to export perfectly into Excel, but the page doesn't update the gridview (since it is not in the UpdatePanel and is not part of the Async Postback).

Preferably, I'd like to find a way to keep the Gridview in the UpdatePanel and get the Export button to work.

Here is the code for my export button:

1protected void btnExportToExcel_Click1(object sender, EventArgs e)2 {3 Response.Clear();4 Response.AddHeader("content-disposition","attachment;filename=HREN_Form_Not_Completed.xls");5 Response.Charset ="";6// If you want the option to open the Excel file without saving than7 // comment out the line below8 // Response.Cache.SetCacheability(HttpCacheability.NoCache);9 Response.ContentType ="application/vnd.xls";10 System.IO.StringWriter stringWrite =new System.IO.StringWriter();11 System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);12 gvReport.RenderControl(htmlWrite);13//Response.Write(cboAssociate.SelectedItem.Text + "_" + cboMonth.SelectedItem.Text + "_" + cboYear.SelectedItem.Text);14 Response.Write(stringWrite.ToString());15 Response.End();1617 }

Any ideas? Thanks!

Here are some sample codes to export to Excel based on Ajax RC 1.0 for your reference.
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Panel ID="pnl2Export" runat="server">
<asp:Table ID="Table1" runat="server" Width="187px">
<asp:TableRow runat="server">
<asp:TableCell runat="server">This will Print if this is successful!</asp:TableCell>
<asp:TableCell runat="server">Export to Excel in a asp:UpdatePanel</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
<asp:Button ID="Postback" runat="server" Text="Export Control (panel)" OnClick="Postback_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Postback" />
</Triggers>
</asp:UpdatePanel>

Behind Codes:

protected void Postback_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ExcelReport.xls");
Response.Charset = "";
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
pnl2Export.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

Wish the above can help you.

Export to Microsoft Excel

Hello,
IE Functionality of Exporting tables to Microsoft Excel doesn't work properly when used with Asp.NET AJAX,

it always exports the first loaded data ignoring any updates done through AJAX Asynchronous post back

i.e. if a page is loaded with data X and then updated using AJAX update panel and
returned data Y then using Export to Microsoft Excel on this page will export
the first data X and not data Y


how can this be fixed?

Appreciate your help in advance.

maybe you can put some code here so that we can know what actually happen.

currently it work fine to me


Hello,

This is a sample of the code unfortunately i wanted to upload the solution but was not able to add files.

I have a master page, default.aspx and dataclass.cs

Appreciate your help in advance.

"C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
"Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> "ScriptManager1" runat="server">
"DropDownList1" runat="server" AutoPostBack="True"> A B

"UpdatePanel1" runat="server"> "GridView1" runat="server" DataSourceID="ObjectDataSource1"> "ObjectDataSource1" runat="server" TypeName="DataClass" SelectMethod="FillData"> "DropDownList1" Name="text" PropertyName="SelectedValue" Type="String">

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){if(!IsPostBack)DropDownList1.SelectedIndex = 1;}}public class DataClass{public DataClass(){//// TODO: Add constructor logic here//}public static DataTable FillData(string text){DataTable dt =new DataTable();dt.Columns.Add("Column");for (int i = 0; i < 100; i++){dt.Rows.Add(new object[] { text });}return dt;}}

Export to Excel problem using UpdatePanels and Response object

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.dll

An 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_Click2protected void ExcelImageButton_Click(object sender, EventArgs e)3 {4// Serialize DataSource5 XmlDocument input =new XmlDocument();6 input = SerializeProductionVariance(dataSourceas IList);78 Response.Clear();9 Response.ContentType ="application/vnd.ms-excel";10 Response.Charset ="";11if (Selection.Count > 1)12 {13 Response.AddHeader("content-disposition","attachment; filename=MultiSelect.xls");14 }15else16 {17 Response.AddHeader("content-disposition","attachment; filename=Selection[0].Name +".xls");18 }1920 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
 

Hi

Make sure that your export button is out side of the update panel.


And that's my problem. The usercontrol is surrounded by an UpdatePanel and has to be surrounded by said UpdatePanel.

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

Saturday, March 24, 2012

Exception During Exporting Data From GridView to Excel File

Hi ,

I am Using Update Panel in my page and this Update panel contains lots of server side controls including GridView ...

Well, i want to Export Data from GridView to Excel or PDF file ... But its not working properly ...

The Code is ...

Response.Clear();

Response.AddHeader("content-disposition","attachement;filename=" +"IFR" + System.DateTime.Now +".xls");

Response.Charset ="";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType ="application/vnd.xls";

StringWriter stringWrite =newStringWriter();

HtmlTextWriter htmlWrite =newHtmlTextWriter(stringWrite);

gvReport.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

The code written is correct but it gives Exception on Execeting the line.. gvReport.RenderControl(htmlWrite);

Exception = < Control 'gvReport' of type 'GridView' must be placed inside a form tag with runat=server. >

And all the controls including GridView are inside the form tag.

Please Help me out to fix that problem i shall be very thankful 2 u.

Its gud if u reply the solution on ... irfan623@dotnet.itags.org.hotmail.com

Thanks .

Irfan Gull

Hi There,

You can't use Response.Write inside of an UpdatePanel.

Why? Because Ajax updates are done using XMLHttpRequest which exchanges xml with server. Resonse.Write adds some string in xml and the xml parser on the client has no idea what to do with such a xml.

Hope it helps!


Basically the Error is at

dgFailureReport.RenderControl(htmlWrite);

When this line executes than Exception occurs...

There is no problem with response.write ...

the problem is with only the above one line code...

Let me paste the code again here... am still having that problem please someone help me ragarding this... thanks in advance...

Response.Clear();

Response.AddHeader("Internal Failure Report","attachement;filename=" +"IFR" + System.DateTime.Now+".xls");

Response.Charset ="";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType ="application/vnd.xls";

StringWriter stringWrite =newStringWriter();HtmlTextWriter htmlWrite =newHtmlTextWriter(stringWrite);

dgFailureReport.RenderControl(htmlWrite); // Here Exception Occurs.

Response.Write(stringWrite.ToString());

Response.End();