Exportar a Excel en ASP.NET

Quisiera saber como exportar de Asp.net un DataGrid a Excel (si saben de alguna otra forma favor de explicarla), me sale un error, esta comentado en la linea donde aparece:

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.ContentType = "application/ms-excel ";
        StringWriter sw = new StringWriter()
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(hw);;// <-- Aqui me sale el error:RegisterForEventValidation can only be called during Render();
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();

    }
      public override void VerifyRenderingInServerForm(Control control)
    {

    }
0
задан 26.12.2016, 22:18
0 ответов