title: Exporting Excel is giving error on button click
body: <p>I am implementing the functionality that on button click the data saved in the gridview should get exported on the button click, I implemented the functionality from <a href="http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx" rel="nofollow">here</a>. </p>
  
  <p>But I am getting error as <em>Control 'mainContent_grdTeacherProfile' of type 'GridView' must be placed inside a form tag with runat=server</em>  at line</p>
  
  <pre><code>grdTeacherProfile.RenderControl(hw);
  </code></pre>
  
  <p>Please see the asps code of the gridview:-</p>
  
  <pre><code>&lt;asp:GridView ID="grdTeacherProfile"
                  runat="server"
                  Width="100%"
                  border="1" Style="border: 1px solid #E5E5E5;"
                  CellPadding="3" FooterStyle-BackColor="#e3e3e3"
                  AutoGenerateColumns="false"
                  AllowPaging="true"
                  CssClass="hoverTable"
                  DataKeyNames="Id"
                  PageSize="4"
                  ShowFooter="false"
                  OnPreRender="PreRenderGrid"
                  HeaderStyle-CssClass="k-grid td"
                  OnDataBound="grdTeacherProfile_DataBound"
                  OnPageIndexChanging="grdTeacherProfile_PageIndexChanging"
                  OnRowDeleting="grdTeacherProfile_RowDeleting"
                  OnRowCommand="grdTeacherProfile_RowCommand"
                  EnableSortingAndPagingCallbacks="false"
                  EmptyDataText="No records found"&gt;
                  &lt;AlternatingRowStyle CssClass="k-alt" /&gt;
                  &lt;Columns&gt;
                      &lt;asp:TemplateField HeaderText="Select" ItemStyle-Width="5" &gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:CheckBox ID="chkDelete" runat="server" onClick="Check_Click(this)" /&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:BoundField DataField="first_name" HeaderText="First Name" ItemStyle-Width="30"  /&gt;
                      &lt;asp:BoundField DataField="last_name" HeaderText="Last Name" ItemStyle-Width="30" /&gt;
                      &lt;asp:BoundField DataField="dob" HeaderText="Date of Birth" ItemStyle-Width="20"  ApplyFormatInEditMode="true" DataFormatString="{0:d}" /&gt;
                      &lt;asp:BoundField DataField="gender" HeaderText="Gender" ItemStyle-Width="20"  /&gt;
                      &lt;asp:BoundField DataField="designation" HeaderText="Designation" ItemStyle-Width="20" /&gt;
                      &lt;asp:BoundField DataField="joining_date" HeaderText="Joining Date" ItemStyle-Width="20"  ApplyFormatInEditMode="true" DataFormatString="{0:d}" /&gt;
                      &lt;asp:BoundField DataField="leaving_date" HeaderText="Leaving Date" ItemStyle-Width="20"  ApplyFormatInEditMode="true" DataFormatString="{0:d}" /&gt;
                      &lt;asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="25" /&gt;
                      &lt;asp:TemplateField HeaderText="Action" HeaderStyle-Width="5%"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:ImageButton ID="btnEdit" AlternateText="Edit" ImageUrl="~/images/edit.png" runat="server" Width="15" Height="15" CommandName="eEdit" CommandArgument='&lt;%# Eval("Id") %&gt;' CausesValidation="false" /&gt;
                              &lt;asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='&lt;%# Eval("Id") %&gt;' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" /&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
                  &lt;PagerStyle ForeColor="#e3e3e3"
                      BackColor="#e3e3e3" CssClass="grid-pagi" /&gt;
                  &lt;PagerTemplate&gt;
                  &lt;table runat="server" id="testTable1" style="width: 100%" class="hoverTable_tbl"&gt;
                      &lt;tr&gt;
                          &lt;td class="col-md-4 pull-left"&gt;
                              &lt;asp:Label ID="MessageLabel"
                                  Text="Select a page:"
                                  runat="server" /&gt;
                              &lt;asp:LinkButton ID="FirstLB" runat="server" CommandName="Page" CommandArgument="First" ToolTip="First" CssClass="btn-pager btn-default"&gt;&lt;&lt;&lt;/asp:LinkButton&gt;
                              &lt;asp:LinkButton ID="PrevLB" runat="server" CommandName="Page" CommandArgument="Prev" ToolTip="Previous" CssClass="btn-pager btn-default"&gt;&lt;&lt;/asp:LinkButton&gt;
                              &lt;asp:DropDownList runat="server" ID="PageDropDownList" AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" CssClass="selectpicker form-control-drp"&gt;&lt;/asp:DropDownList&gt;
  
                              &lt;asp:LinkButton ID="NextLB" runat="server" CommandName="Page" CommandArgument="Next" ToolTip="Next" CssClass="btn-pager btn-default"&gt;&gt;&lt;/asp:LinkButton&gt;
                              &lt;asp:LinkButton ID="LastLB" runat="server" CommandName="Page" CommandArgument="Last" ToolTip="Last" CssClass="btn-pager btn-default"&gt;&gt;&gt;&lt;/asp:LinkButton&gt;
                          &lt;/td&gt;
                          &lt;td class="col-md-3"&gt;
                              &lt;div&gt;
                              &lt;div class="pull-left"&gt;&lt;asp:Label ID="PageSizeLabel" CssClass="page-size" runat="server" Text="Select Page Size: "&gt;&lt;/asp:Label&gt;
                                  &lt;asp:DropDownList ID="ddlPageSize" runat="server" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" AutoPostBack="true" CssClass="selectpicker form-control-drp"&gt;
                                  &lt;%-- &lt;asp:ListItem Value="0" Text="0" /&gt;--%&gt;
                                  &lt;asp:ListItem Value="1" Text="1" /&gt;
                                  &lt;asp:ListItem Value="2" Text="2" /&gt;
                                  &lt;asp:ListItem Value="3" Text="3" /&gt;
                              &lt;/asp:DropDownList&gt;
                              &lt;/div&gt;
                              &lt;div&gt; 
                                   &lt;asp:Label ID="CurrentPageLabel" CssClass="view" runat="server" /&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/PagerTemplate&gt;
  
                  &lt;RowStyle /&gt;
              &lt;/asp:GridView&gt;
  </code></pre>