带有ITemplate字段的GridView导出到Excel

时间:2021-03-30 14:46:17

I have a GridView wich has some programmatically Template fields added to it on Page_Load and on every PostBack (because, for some reason, these fields loose their value on PostBack).

我有一个GridView,它在Page_Load和每个PostBack上都有一些以编程方式添加的模板字段(因为出于某种原因,这些字段在PostBack上失去了它们的值)。

It worked fine, so I didn't thought anymore about the PostBack issue.

它工作正常,所以我没有想到PostBack问题。

Now I'm trying to export that GridView to Excel. I'm using the following code:

现在我正在尝试将GridView导出到Excel。我正在使用以下代码:

protected void ExportToExcel_Click(object sender, EventArgs e)
{
    string attachment = "attachment; filename=ExcelFile.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.xls";
    Response.Charset = "utf-8";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    // Create a form to contain the grid
    HtmlForm frm = new HtmlForm();
    this.gridValores.Parent.Controls.Add(frm);
    frm.Attributes["runat"] = "server";
    frm.Controls.Add(this.gridView);
    frm.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}

The problem is that all the columns are rendered to the Excel file except the Template fields, that are left blank.

问题是所有列都呈现给Excel文件,但模板字段除外,这些字段留空。

I think that the problem is related with the fact that the Template fields loose their values on each PostBack. I've searched about it and found people saying that these dynamic fields should be added on Page_Init, not on Page_Load. I'm not sure this is true, nevertheless I need to create those fields based on information that I get through HttpRequest, that isn't there on Init, if I'm not mistaken.

我认为这个问题与Template字段在每个PostBack上的值都松散有关。我搜索过它并发现人们说这些动态字段应该在Page_Init上添加,而不是在Page_Load上添加。我不确定这是否属实,但是我需要根据我通过HttpRequest获得的信息来创建这些字段,如果我没有弄错的话,那就不在Init上。

Can someone help me in this?

有人可以帮助我吗?

1 个解决方案

#1


I've found a lot of people with problems like this one, but very few answers that could help me.

我发现很多人都有像这样的问题,但很少有人可以帮助我。

Until this one: http://forums.asp.net/p/1229438/2216336.aspx#2216336

直到这一个:http://forums.asp.net/p/1229438/2216336.aspx#2216336

This can really solve my problem. I've done some testing and it works. He managed to save the Template fields in cache and restore them on Page_Load. Clever.

这可以真正解决我的问题。我做了一些测试,但它确实有效。他设法将模板字段保存在缓存中并在Page_Load上恢复它们​​。聪明。

If someone finds a better solution, please post it here. It can be very useful for other fellow developers that are banging their heads on the wall.

如果有人找到更好的解决方案,请在此处发布。对于那些正在敲打墙壁的其他开发人员来说,它非常有用。

#1


I've found a lot of people with problems like this one, but very few answers that could help me.

我发现很多人都有像这样的问题,但很少有人可以帮助我。

Until this one: http://forums.asp.net/p/1229438/2216336.aspx#2216336

直到这一个:http://forums.asp.net/p/1229438/2216336.aspx#2216336

This can really solve my problem. I've done some testing and it works. He managed to save the Template fields in cache and restore them on Page_Load. Clever.

这可以真正解决我的问题。我做了一些测试,但它确实有效。他设法将模板字段保存在缓存中并在Page_Load上恢复它们​​。聪明。

If someone finds a better solution, please post it here. It can be very useful for other fellow developers that are banging their heads on the wall.

如果有人找到更好的解决方案,请在此处发布。对于那些正在敲打墙壁的其他开发人员来说,它非常有用。