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
Solved

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

Posted on 2011-09-30
4
393 Views
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.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.Html32TextWriter oHTML32TextWriter = new System.Web.UI.Html32TextWriter(oStringWriter);
        GVExceptionsL30.RenderControl(oHTML32TextWriter);        
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

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">
                                </asp:ContentPlaceHolder>
                            </div>
                        </form>
                    </div>
                    <div class="portletBottom">
                        <div class="bottomRight">
                            &nbsp;</div>
                    </div>
                </div>
            <div class="fillerTwenty">&nbsp;</div>
            </td>

Open in new window

0
Comment
Question by:Laxman1011
4 Comments
 

Author Comment

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

Accepted Solution

by:
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
http://johnnycoder.com/blog/2008/07/25/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.");
        return;
    }

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

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

    // SetCacheability doesn't seem to make a difference (see update)
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);  

    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
    ClearControls(AccountGrid);

    // 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();
    Controls.Add(form);
    form.Controls.Add(AccountGrid);
    form.RenderControl(htmlWriter);

    Response.Write(stringWriter.ToString());
    Response.End();

Open in new window

0
 
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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