JAYRAM

JAYRAM

  • NA
  • 272
  • 215.4k

export gridview to excel ......error

Feb 3 2013 11:02 AM




 protected void Button1_Click(object sender, EventArgs e)
    {
         Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=GridView_to_Excel.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
        
        gv.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();
        }

    }

.....................................................................................................
error .....


Exception Details: 
System.Web.HttpException: Control 'gv' of type 'GridView' must be placed inside a form tag with runat=server.

Source Error: 

Line 122:        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
Line 123:        //GridView1.RenderControl(HtmlTextWriter);
Line 124:        gv.RenderControl(HtmlTextWriter);
Line 125:        Response.Write(StringWriter.ToString());
Line 126:        Response.End();

Answers (4)