• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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

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
Laxman1011
Asked:
Laxman1011
1 Solution
 
Laxman1011Author Commented:
How to export entire dataset instead of the filtered data to xls
0
 
Bob LearnedCommented:
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
 
Amandeep Singh BhullarCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now