Tuesday, July 20, 2010

"asp.net, C#" Control '******_*******_*******' of type 'GridView' must be placed inside a form tag with runat=server.

        //Problem:
        "Control 'ctl00_cphBody_dgReport' of type 'GridView' must be placed inside a form tag with runat=server."

        protected void btnExcel_Click(object sender, System.EventArgs e)
        {
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = null;
            Response.AddHeader("content-disposition", "attachment;filename=ActivatedUserDetails.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/xls";
            htmlWrite = new HtmlTextWriter(stringWrite);
            dgReport.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }

        //Solution: Step-1:
        //on page.aspx page set EnableEventValidation to false
        EnableEventValidation="false"
        //on page.aspx page set EnableEventValidation to false

        //Solution: Step-2:
        //on page.aspx.cs page add the following code snippet.
        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        //and its all done :-)

No comments:

Post a Comment