Welcome to the CodeSmith Community!

Re: ExportToExcel error

Support Forums

Feel free to ask any questions about CodeSmith here.

ExportToExcel error

  • rated by 0 users
  • This post has 5 Replies |
  • 2 Followers
  • When trying to Export EntityGridView To Excel, get an error:

    RegisterForEventValidation can only be called during Render();

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: RegisterForEventValidation can only be called during Render();

    Source Error:

    Line 728:            {
    Line 729:                HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    Line 730:                htmlForm.RenderControl(htmlWriter);
    Line 731:                htmlWriter.Flush();
    Line 732:

  • Amy,

    In the page directive try setting EnableEventValidation="false"

     

     

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

  • Thanks Mike for you reply. It works, but I can't set EnableEventValidation = "false" for security reason.

     

     

  • Amy,

    This issue is discussed here as well http://forums.asp.net/1/1064482/ShowThread.aspx not all suggestions work for everyone.

     

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

  • Since I couldn't access Mike's link, I did some of my own investigating and settled on the solution employed by Matt Berseth (replacing controls with their actual values):

    mattberseth.com/.../export_gridview_to_excel_1.html

    Specifically, I adapted the Export function in EntityGridView.cs as follows (I'm using NetTiers 2.3.1):

    /// <summary>
            /// Exports the datagrid to an Excel file with the name of the datasheet provided by the passed in parameter
            /// ala http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
            /// </summary>
            public virtual void Export(string reportName, Page currentPage, GridView gridView)
            {
                currentPage.Response.Clear();
                currentPage.Response.Buffer = true;
    
                currentPage.Response.AddHeader("Content-Disposition", "attachment; filename=" + reportName);
                currentPage.Response.ContentType = "application/vnd.ms-excel";
                currentPage.Response.ContentEncoding = System.Text.Encoding.UTF8;
                currentPage.Response.Charset = "";
                currentPage.EnableViewState = false;
    
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        //  Create a table to contain the grid
                        Table table = new Table();
    
                        //  include the gridline settings
                        table.GridLines = gridView.GridLines;
    
                        //  add the header row to the table
                        if (gridView.HeaderRow != null)
                        {
                            PrepareControlForExport(gridView.HeaderRow);
                            table.Rows.Add(gridView.HeaderRow);
                        }
    
                        //  add each of the data rows to the table
                        foreach (GridViewRow row in gridView.Rows)
                        {
                            PrepareControlForExport(row);
                            table.Rows.Add(row);
                        }
    
                        //  add the footer row to the table
                        if (gridView.FooterRow != null)
                        {
                            PrepareControlForExport(gridView.FooterRow);
                            table.Rows.Add(gridView.FooterRow);
                        }
    
                        //  render the table into the htmlwriter
                        table.RenderControl(htw);
    
                        //  render the htmlwriter into the response
                        htw.Flush();
    
                        currentPage.Response.Write(sw.ToString());
                        currentPage.Response.End();
                    }
                }
            }
    
    
    Perhaps something along those lines could meet your needs.
  • Oops, left out some code...

    /// <summary>

        /// Replace any of the contained controls with literals

        /// </summary>

        /// <param name="control"></param>

        private static void PrepareControlForExport(Control control)

        {

            for (int i = 0; i < control.Controls.Count; i++)

            {

                Control current = control.Controls[i];

                if (current is LinkButton)

                {

                    control.Controls.Remove(current);

                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));

                }

                else if (current is ImageButton)

                {

                    control.Controls.Remove(current);

                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));

                }

                else if (current is HyperLink)

                {

                    control.Controls.Remove(current);

                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));

                }

                else if (current is DropDownList)

                {

                    control.Controls.Remove(current);

                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));

                }

                else if (current is CheckBox)

                {

                    control.Controls.Remove(current);

                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));

                }

     

                if (current.HasControls())

                {

                    GridViewExportUtil.PrepareControlForExport(current);

                }

            }

        }

Page 1 of 1 (6 items)