System.web.httpException: ‘Control ‘MainContent_GridView1′ of type GridView must be placed inside a form tag with runat=server’

GridView must be placed inside a form tag with runat server

System.web.httpException: ‘Control ‘MainContent_GridView1’ of type GridView must be placed inside a form tag with runat=server’

Recently I was using GridView in asp.net and was trying to implement export to word functionality in asp.net application. But when I click on the export button it gave me error as System.web.httpException: ‘Control ‘MainContent_GridView1’ of type GridView must be placed inside a form tag with runat=server’.

SharePoint deveopment training course

Below is the code I was using for downloading in word format.

.aspx code (Gridview):

<asp:GridView ID=”GridView1″ runat=”server”></asp:GridView><br />
<asp:Button ID=”btnDownload” runat=”server” Text=”Download” OnClick=”btnDownload_Click” />

.aspx.cs code for downloading in word format:



private void ExportGridToWord()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = “”;
string FileName = “EnjoySharePoint” + DateTime.Now + “.doc”;
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = “application/msword”;
Response.AddHeader(“Content-Disposition”, “attachment;filename=” + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();

}

But it throws the error at line GridView1.RenderControl(htmltextwrtter); The error looks like below:

GridView must be placed inside a form tag with runat server
GridView must be placed inside a form tag with runat server

Solution:

The solution to resolve the issue is to add the below event in the code file. You do not need to write any business logic here.

public override void VerifyRenderingInServerForm(Control control)
{

}

After this the issue will not come. Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →