Solved

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

Posted on 2011-09-30
4
382 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now