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’.
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.Buffer = true;
Response.Charset = “”;
string FileName = “EnjoySharePoint” + DateTime.Now + “.doc”;
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.ContentType = “application/msword”;
Response.AddHeader(“Content-Disposition”, “attachment;filename=” + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
But it throws the error at line GridView1.RenderControl(htmltextwrtter); The error looks like below:
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.
SharePoint Online FREE Training
JOIN a FREE SharePoint Video Course (3 Part Video Series)