Showing posts with label export. Show all posts
Showing posts with label export. Show all posts

Monday, March 26, 2012

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

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 within a AJAX Updatepanel

Hi, I'm getting an "pagerequestmanagerparsererrorexception" error when trying to export a gridview to an excel spreadsheet within an updatepanel.

here is the code below ;

Protected Sub Export2Excel() Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=TestExcel.xls") Response.Charset = "" 'If you want the option to open the Excel file without saving than 'comment out the line below Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.xls" Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter() Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite) Try Results_GridView.RenderControl(htmlWrite) Response.Write(stringWrite.ToString) Catch ex As Exception End Try Response.End() End Sub
 Any help would be greatly appreciate..
 Thanks,
Clint 

You need to open up the excel in a pop up window or in an iframe. You can not send that content down through the Ajax Request and expect it to download on the users computer.

Eric


Hi Clint,

I think the simplest way is do not put the button inside any update panel.

Hope this helps.


Hi,

I am using a link button within the update panel, this is when I am getting the error.

Thanks


i could make it, adding an

<Triggers>
<asp:PostBackTrigger ControlID="ExportingButton" />
</Triggers>

to the updatepanel... hope this helps!!!

by!


Hi, the problem it's that when you use ajax you can't continue using theresponse.write,because the big idea of Ajax is not write all the page, only someparts, well the solution it's very easy, add an page and send StringWrite to a session variable, and add an Response.Redirect to the new page without ajax and in the load of the new page write your Response.write, and that's all, that will only open a windows in the client side asking if want to save or open the file, hope this be usefull fou U.


I've handled this in this post:

http://forums.asp.net/t/1047851.aspx

Hope it helps!

Bebandit

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

Export to excel and UpdatePanel problems

As I have noticed myself and while looking around for a good answer, a normal asp button to export to excel does not work inside UpdatePanels since Response lines are not viable inside UpdatePanels. It throws an exception that states something to same affect, "common causes for this error are when the response is modified", etc.

I have looked around and was not able to find a complete answer to work around this problem. The shortened code that I am using is below:

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
frm.Controls.Add(gv)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.Flush():Response.Close():Response.End()

I guess the question is obvious... Is there a clean way around this besides taking the button out of the UpdatePanel? I say this since it does look silly to have one button hanging out by itself in the corner of the page somewhere... I am still new to this so I would much appreciate it if you could "dumb" down the answers a bit.

A bit off topic but would there be a way to handle this on the master page? I believe I just asked two questions ( 1. Update Panel and export to Excel issues, and 2. How to handle this in a master page) but I am mainly focused on the Export issue.

I am able to provide further information as needed. Thanks in advance to all.

First I believe you don't want to add an UpdatePanel around the response.

However why are you doing this? Response.Flush():Response.Close():Response.End()?

Just use Flush but do not close the Response


I saw both ways on the web in different threads that use both ways, without the close and end and with the close and end. However, having the habit of closing what I open, I naturally went with closing it.

BTW, the close and end did not seem to be the cause of the problem as the export as the same problem persists.


I met this problem too. when I put the export buttom into a updatepannel, it will display some javascript error. I think the most possibility of this problem that is you put the Response.contentType in the code as updatePannel use the XMLHTTP to aysn communicate with the server.

Response.ContentType = "application/vnd.ms-excel"

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")

Big Smile

Export excel

How can export grid in excel or word using ajax?

hello.

see if this helps:

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

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();