Wednesday, May 16, 2007 4:34 PM
I'm banging my head against the wall on this one...
I have a Sharepoint 2007 webpart which displays a datagrid. I need to implement functionality to export the data from the grid into an Excel spreadsheet. So in my Export function in my code behind, I do this:
[Start Code]
Context.Response.Clear();
Context.Response.AddHeader("content-disposition", "attachment;filename=myexcelfile.xls");
Context.Response.ContentType = "application/vnd.xls"
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvResults.RenderControl(htw);
Context.Response.Write(sw.ToString());
Context.Response.End();
[End Code]
This works perfectly...I get the prompt about the file, I can open it up or save it...its great.
But the problem is after I deal with the file. The controls on my webpart are dead when this loads. The buttons that perform a search or to do a new export don't post back at all. If I refresh the page, then they're active again...but this is obviously not desirable.
The only thing I can think of is htat because we're overriting the Response object, we're kyboshing the response that handles the other controls...but if I run this within a regular ASP.NET page it works perfectly, even after dealing with the Excel spreadsheet. So this seems to be only an issue with Sharepoint web-parts.
I also toyed with the idea of using a client script to accomplish this, but then we run into issues of browser security (becuase the client script has to create an Excel object).
I'm at a loss...is this even possible? Is there something I'm missing about how Web-Parts render that's different from ASP.NET apps? I'm looking for some help here...all the pieces we need work on their own, the just don't want to work together.
Thanks,
D