使用response.write()时出现错误

发布于 2024-12-06 15:09:37 字数 1646 浏览 1 评论 0原文

我需要在客户端计算机中将文件另存为 .csv。我正在使用下面的代码。

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!";                
    }

我没有使用更新面板

当我点击按钮时出现以下错误

Sys.WebForms.PageRequestManagerParserErrorException:消息 无法解析从服务器收到的信息。造成这种情况的常见原因 错误是通过调用 Response.Write() 修改响应时, 响应过滤器、HttpModules 或服务器跟踪已启用。

如果你能帮助我解决这个问题,那对我来说将是一个很大的帮助。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2024-12-13 15:09:37

我解决了。母版页中的 Ajax 脚本管理器造成了这个问题。删除后,功能正常。但标签没有更新,因为响应不是针对该页面的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文