Saturday, 15 January 2011

c# - Response.End() generates an error when exporting gridview to excel file -



c# - Response.End() generates an error when exporting gridview to excel file -

i have code:

protected void ibtgeneratereport_click(object sender, imageclickeventargs e) { string filename = "report.xls"; stringwriter stringwriter = new stringwriter(); htmltextwriter htmlwrite = new htmltextwriter(stringwriter); datagrid datagrd = new datagrid(); datagrd.datasource = odslsraudit; datagrd.databind(); datagrd.rendercontrol(htmlwrite); system.io.streamwriter vw = new system.io.streamwriter(filename, true); stringwriter.tostring().normalize(); vw.write(stringwriter.tostring()); vw.flush(); vw.close(); writeattachment(filename, "application/vnd.ms-excel", stringwriter.tostring()); } public static void writeattachment(string filename, string filetype, string content) { httpresponse response = system.web.httpcontext.current.response; response.clearheaders(); response.appendheader("content-disposition", "attachment; filename=" + filename); response.contenttype = filetype; response.write(content); response.end(); }

but response.end() gives me below error:

microsoft jscript runtime error: sys.webforms.pagerequestmanagerparsererrorexception: message received server not parsed. mutual causes error when response modified calls response.write(), response filters, httpmodules, or server trace enabled. details: error parsing near '<table cellspacing="'.

the response.write(content) has right info can see. no save/open dialog box appear, besides above error.

i'm using updatepanels.

add page_load() method

assuming ibtgeneratereport button

protected void page_load(object sender, eventargs e) { scriptmanager scriptmanager = scriptmanager.getcurrent(this.page); scriptmanager.registerpostbackcontrol(this.ibtgeneratereport); //further code goes here.... }

explanation:

the updatepanel command uses asynchronous postbacks command parts of page rendered. using whole bunch of javascript on client , whole bunch of c# on server. rendered out using special format javascript on client can understand. if mess format rendering things outside of render phase of page, format messed up.

why keeping getting pagerequestmanagerparsererrorexception?

well, chances you're doing 1 of things mentioned in error message. here mutual reasons , why don't work:

calls response.write(): calling response.write() straight bypassing normal rendering mechanism of asp.net controls. bits write going straight out client without farther processing.

response filters /httpmodules : similar response.write(), can alter rendering in such way updatepanel won't know.

server trace enabled: trace written out using response.write(), , such messes special format utilize updatepanel.

calls server.transfer(): unfortunately, there's no way observe server.transfer() called. means updatepanel can't intelligent when calls server.transfer(). response sent client html markup page transferred. since html , not special format, can't parsed, , error.

solution: 1 way avoid parse error regular postback instead of asynchronous postback calling scriptmanager.registerpostbackcontrol()

please refer finish explanation , other solutions ellon lipton's blog here

c# asp.net gridview export-to-excel

No comments:

Post a Comment