Home » » Exception during Export to Excel?

Exception during Export to Excel?

Written By M.L on செவ்வாய், 2 ஆகஸ்ட், 2011 | ஆகஸ்ட் 02, 2011

I’m trying to export data from DataGridView into Excel sheet, so I used the following code, but at bolded and underlined line I’m getting an exception that says “{"RegisterForEventValidation can only be called during Render();"}”

How should I solve this?


private void ExportGridView()
    {
        try
        {
            string attachment = "attachment; filename = Report.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm frm = new HtmlForm();
            GridView1.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(GridView1);

            frm.RenderControl(htw);
            //GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

        catch (Exception e1)
        {
            Response.Write(e1);
        }
    }

1 comments:

  1. The solution for this is by setting the “EnableEventValidation” as False

    It worked for me. I thank everyone for their effort in letting me know the solution.

    பதிலளிநீக்கு

Popular Posts

General Category