Response.write is not working

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
10/21/2012 10:19:09 AM
Gravatar
Total Posts 4

Response.write is not working

public void ExportToExcel(DataTable dt, string filename)
{
if (dt.Rows.Count > 0)
{
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();

//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
HttpContext.Current.Response.Flush();
}
}

I am using above code to export datatable to excel but response.write is not working

 I just want to export datatable to excel file from my custom control gridview.

10/22/2012 9:32:01 AM
Gravatar
Total Posts 18439

Re: Response.write is not working

You need to understand that ASP.NET is not the same as Classic ASP you cannot use Response.Write any old place you want, it breaks the control rendering model when used incorrectly. You cannot do it in an UpdatePanel for example. To download a file you can use Response.Write but not while also rendering other controls or html, a download request must be all by itself and the response should contain only the file and nothing else.

There is example code in mojoPortal similar to what you are trying to do but exporting to csv. you can study the ExportaDataTableToCsv method in the ExportHelper.cs and an example where it is called is in btnExport_Click in LetterSubscribers.aspx.cs

To figure out what is going on you should set breakpoints and step through your code.

Hope that helps,

Joe

10/22/2012 12:23:49 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Response.write is not working

I found this post helpful when creating a feature that exports to CSV from a mojoGridView, and it works great.

Hope that helps,

Jamie

10/23/2012 11:04:24 PM
Gravatar
Total Posts 4

Re: Response.write is not working

Thanks Joe,

                     I dont know how i missed this nut i was using that method in update panel....Sealed BTW thank you so much for the instant help really appreciate it. I got the job done by using NPOI 

You must sign in to post in the forums. This thread is closed to new posts.