locked
Error - {"RegisterForEventValidation can only be called during Render();"} RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I am getting above error when export to excel . I have this code in Content Page. Secondly i want to start export from second column

     public override void VerifyRenderingInServerForm(Control control)
            {
                /* Verifies that the control is rendered */
            }
    
            private void ExportGridToExcel()
            {
    
               Response.Clear();  
               Response.Buffer = true;  
               Response.ClearContent();  
               Response.ClearHeaders();  
               Response.Charset = "";  
               string FileName ="Vithal"+DateTime.Now+".xls";  
               StringWriter strwritter = new StringWriter();  
               HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);        
               Response.Cache.SetCacheability(HttpCacheability.NoCache);  
               Response.ContentType ="application/vnd.ms-excel";    
               Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
               gvw.GridLines = GridLines.Both;
               gvw.HeaderStyle.Font.Bold = true;
               gvw.RenderControl(htmltextwrtter);  
               Response.Write(strwritter.ToString());  
               Response.End();      
    }

    Thanks

    Friday, November 23, 2018 2:50 PM

Answers

  • User-893317190 posted

    Hi JagjitSingh,

    About the error ,you could try to add  EnableEventValidation="false" in your aspx.

    <%@ Page Language="C#" EnableEventValidation="false"  AutoEventWireup="true" CodeBehind="ExportExcelFromSecondColumn.aspx.cs" Inherits="MyWebFormCases.Office.ExportExcelFromSecondColumn" %>

    If you want to  start export from second column, you could remove the first column of your gridview or just make it invisible in your exporting method.

    Below is my code.

     <form id="form1" runat="server">
            <asp:GridView ID="gvw" runat="server" AutoGenerateColumns="False" DataKeyNames="SID" DataSourceID="SqlDataSource1" >
                <Columns>
                    <asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" ReadOnly="True" SortExpression="SID" />
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Fruit %>" SelectCommand="SELECT [SID], [Name], [City] FROM [Suppliers]"></asp:SqlDataSource>
              <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
      

    Code behind.

      protected void Button1_Click(object sender, EventArgs e)
            {
            
                ExportGridToExcel();
            }
           
    
            private void ExportGridToExcel()
            {
    
                Response.Clear();
                Response.Buffer = true;
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Charset = "";
                string FileName = "Vithal" + DateTime.Now + ".xls";
                StringWriter strwritter = new StringWriter();
                HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
                gvw.GridLines = GridLines.Both;
               // gvw.Columns.RemoveAt(0);
                gvw.Columns[0].Visible = false;
                gvw.DataBind();
                gvw.HeaderStyle.Font.Bold = true;
                gvw.RenderControl(htmltextwrtter);
                Response.Write(strwritter.ToString());
                Response.End();
            }
            public override void VerifyRenderingInServerForm(Control control)
            {
                /* Verifies that the control is rendered */
            }

    The result

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 26, 2018 3:18 AM