Export to XLS in ASP.Net(Master already has FORM runat = server)

Posted on 2011-09-30
Last Modified: 2012-05-12
ERROR : Control 'ctl00_ContentPlaceHolder2_GVExceptionsL30' of type 'GridView' must be placed inside a form tag with runat=server.

The master page has the FORM tag as required.
However i am getting the above error while i am trying to export data to excel
CODE :     protected void btnExportXls_Click(object sender, EventArgs e)
        ExportToExcel("Report.xls", GVExceptionsL30);

    protected void ExportToExcel(string strFileName, GridView dg )
        Response.Buffer = true;
        Response.ContentType = "application/";
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.Html32TextWriter oHTML32TextWriter = new System.Web.UI.Html32TextWriter(oStringWriter);

MASTERPAGE : code snippet
<td width="85%" valign="top">
                <div id="twoLeft_Right">
                    <div id="twoLeft_Right_Inner">
                        <a name="content"></a>
                        <div class="fillerTen">&nbsp;</div>
                        <form id="form1" runat="server">
                            <div style="width: 100%">
                                <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
                    <div class="portletBottom">
                        <div class="bottomRight">
            <div class="fillerTwenty">&nbsp;</div>

Open in new window

Question by:Laxman1011
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 36894905
How to export entire dataset instead of the filtered data to xls
LVL 96

Accepted Solution

Bob Learned earned 500 total points
ID: 36896993
The simple solution is to use an HtmlForm, as shown here:

Export GridView to Excel within an UpdatePanel
// Reference your own GridView here
    if (AccountGrid.Rows.Count > 65535)
        DisplayError("Export to Excel is not allowed" +
            "due to excessive number of rows.");

    string filename = String.Format("Results_{0}_{1}.xls",
        DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString());

    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    Response.Charset = "";

    // SetCacheability doesn't seem to make a difference (see update)

    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter stringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    // Replace all gridview controls with literals

    // Throws exception: Control 'ComputerGrid' of type 'GridView'
    // must be placed inside a form tag with runat=server.
    // ComputerGrid.RenderControl(htmlWrite);

    // Alternate to ComputerGrid.RenderControl above
    System.Web.UI.HtmlControls.HtmlForm form
        = new System.Web.UI.HtmlControls.HtmlForm();


Open in new window

LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 37932333
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question