Control ctl00_ContentMain_ xxxxx of type ‘GridView’ must be placed inside a form tag with runat=server

Recently I wrote some simple code to Export the contents of a GridView to Excel. You know, the basic stuff like changing the type to application/ms-excel content and removing Images from the GridView (so that they don’t appear as missing images in the Excel sheet) and converting Hyperlinks to Labels.

The Problem : But when I ran my code, I got the dreaded error :Control ctl00_ContentMain_ xxxxx of type ‘GridView’ must be placed inside a form tag with runat=server

The Solution :  Just place this code in your codebehind. And that will fix ya! :

public override void VerifyRenderingInServerForm(Control control) {}

The Explanation :
Asp.net calls VerifyRenderingInServerForm to ensure that every single server control is being rendered inside a <form runat=”server”> tag. Which makes sense for a web page.  But it doesn’t make sense for an Excel sheet. Since, we just want to send the Gridview contents to the client and not the Form tag.

All we are doing here is overriding this Method and falsely reporting to Asp.Net that our Control is actually being rendered inside a <Form Runat=’server’> tag.

Hope it helps…

6 thoughts on “Control ctl00_ContentMain_ xxxxx of type ‘GridView’ must be placed inside a form tag with runat=server

  1. Control ctl00_ContentMain_ xxxxx of type ‘GridView’ must be placed inside a form tag with runat=server

    public override void VerifyRenderingInServerForm(Control control) {}

    not exporting in XL????????????
    Help me as soon as possible

    • Recently I wrote some simple code to Export the contents of a GridView to Excel.

      same problem i m facing but the exception i m not is missing of directive and name space but i have already included the name space..

      can anyone help me

  2. grdnos.DataSource = dt1 ‘ MY DATASOURCE

    grdnos.RenderBeginTag(hw)

    grdnos.RenderControl(hw)

    grdnos.RenderEndTag(hw)

    Response.Write(tw.ToString)

    Response.End()

    public override void VerifyRenderingInServerForm(Control control) {}

    not exporting in XL????????????
    Help me as soon as possible

Leave a reply to sarah Cancel reply