Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview export to Excel, error MainContent_Grid must be placed inside a form tag with runat=server.

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
It looks like the 'MainContent_GridView1' is inside the run at server tag, What am I missing?

Code:
        Dim queryTrans As String = CreateExportToExcelSQLString(TheVendor, TheMonth)
        Me.GridView1.DataSource = PopulateGridwithSQL(queryTrans)
        Me.GridView1.DataBind()
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("Content-Disposition", "attachment; filename=MyExcelFileName.xls")
        Dim writer As New System.IO.StringWriter()
        Dim html As New System.Web.UI.HtmlTextWriter(writer)
        Me.GridView1.RenderControl(html)  ' <<<<< Error happens on this line
        Response.Write(writer)
        Response.End()
HTML markup
Code:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="ExportToExcel.aspx.vb" Inherits="SOWTimeReporting.ExportToExcel" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
[b][COLOR=red]<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">[/color][/b]
    Choose Vendor
    <asp:DropDownList ID="ddlChooseVendor" runat="server" AutoPostBack="True">
    </asp:DropDownList>
&nbsp;<br />
    Choose Month&nbsp; 
    <asp:DropDownList ID="ddlChooseMonth" runat="server" AutoPostBack="True">
    </asp:DropDownList>
&nbsp;&nbsp;
    <asp:Button ID="btnExportToExcel" runat="server" Text="Export To Excel" 
        Width="129px" />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</asp:Content>
Error details
-------------------
System.Web.HttpException was unhandled by user code
ErrorCode=-2147467259
Message=Control 'MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Source=System.Web
WebEventCode=0
StackTrace:
at System.Web.UI.Page.VerifyRenderingInServerForm(Control control)
at System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer, Boolean renderPanel)
at System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at SOWTimeReporting.ExportToExcel.btnExportToExcel_Click(Object sender, EventArgs e) in C:\Users\z533560\Documents\Visual Studio 2010\Projects\SOWTimeReporting\ExportToExcel.aspx.vb:line 204
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:


DougP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top