Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-30
4
Medium Priority
?
399 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
[X]
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
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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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