开发者

Getting Error when using response.write()

开发者 https://www.devze.com 2023-04-07 06:25 出处:网络
I need to save a file as .csv in client machine. I am using the code below. protected void btnGen_Click(object sender, EventArgs e)

I need to save a file as .csv in client machine. I am using the code below.

protected void btnGen_Click(object sender, EventArgs e)
{
    try
    {
        string fileName = "test";
        string attachment = "attachment; filename=" + fileName + ".csv";
        List<string> lst1 = new List<string> { "wert", "fds", "hjgfh", "hgfhg" };
        List<string> lst2 = new List<string> { "hgfds", "hdfg", "yretg", "treter" };
        List<string> lst3 = new List<string> { "hgfdgf", "gfdg", "gfdgf", "ghfdg" };
        List<List<string>> lst = new List<List<string>> { lst1, lst2, lst3 };

        StringBuilder sb = new ExportFacade().GenerateStringBuilder(lst);

        Response.ContentType =  "text/csv" ;
        lblProgress.ForeColor = System.Drawing.Color.Green;
        lblProgress.Text = "File created and write successfully!";    
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        Response.Write(sb.ToString());
        lblProgress.ForeColor = System.Drawing.Color.Green;
        lblProgress.Text = "File created and write successfully!";
        Response.Flush();
        Response.End();               
    }
    catch (Exception ex)
    {
        lblProgress.ForeColor = System.Drawing.Color.Red;
        lblProgress.Text = "File Saving Failed!";                
    }

}

I am not using update panels.

When I click on the button I get the following error

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.

It will be a开发者_运维技巧 great help to me if you can help me to get rid of this problem. thank you.


I solved it. Ajax script manager in the master page has created the problem. After removing it, the function worked properly. But labels are not being updated as response is not for the page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号