Problem: registerForEventValidation can only be called during render () execution; result: registerForEventValidation can only be called during render () execution

Source: Internet
Author: User

registerForEventValidation can only be called during Render () execution

When you export execl or Word, the registerforeventvalidation error is called only during Render () execution.
The following 2 methods are found on the web and are not applied:
1. Modify the Web. config (not recommended)

<pages enableEventValidation = "false" ></pages>

2. Edit the page directly in the export EXECL

<%@ Page Language="C#"AutoEventWireup="true" CodeFile="ExportWordByIO.aspx.cs" Inherits="_ Default" enableeventvalidation = "false" %>

My own application of the processing method:

Another situation is that because the controls exported to an Excel file contain internal controls (such as Linkbutton,button, hyperlinks, and so on), these internal controls need to be converted to text.
Use the clearcontrols () method to clear the internal controls before exporting the data.

///<summary>
///Export Excel Features
///</summary>
///<param name= "Sender" ></param>
///<param name= "E" ></param>
Protectedvoid Btn_excel_click (Object sender, EventArgs e)
{
//Export all data, cancel paging
Gridview_batchquality.allowpaging =False
Gridview_batchquality.showfooter =False

Bindgridview ();

Response.Clear ();
Response.Buffer =True
Response.Charset ="Utf-8";
Response.appendheader ("Content-disposition","Attachment;filename="+ System.Web.HttpUtility.UrlEncode ("Export"+ System.DateTime.Now.Date.ToString ("YyyyMMdd")) +". xls");
response.contentencoding = System.Text.Encoding.GetEncoding ("Utf-8");//Set output stream to Simplified Chinese

Response.ContentType ="Application/ms-excel";//Set the output file type to excel file.
This. EnableViewState =False
System.Globalization.CultureInfo Mycitrad =New System.Globalization.CultureInfo ("Zh-cn",true);
System.IO.StringWriter Ostringwriter =New System.IO.StringWriter (Mycitrad);
System.Web.UI.HtmlTextWriter Ohtmltextwriter =New System.Web.UI.HtmlTextWriter (Ostringwriter);

Clearcontrols (gridview_batchquality);
This. Gridview_batchquality.rendercontrol (Ohtmltextwriter);
Response.Write (Ostringwriter.tostring ());
Response.End ();

//To restore the paging display
Gridview_batchquality.allowpaging =True
Gridview_batchquality.showfooter =True
Bindgridview ();
}

///<summary>
///GridView if you need to implement the export Excel feature, the function needs to be overloaded
///</summary>
///<param name= "Control" ></param>
PublicOverridevoid Verifyrenderinginserverform (Control control)
{
//Confirms that a HtmlForm control is rendered for
}

///<summary>
///Clears all controls in the control to export Excel
///</summary>
///<param name= "Control" ></param>
Privatevoid Clearcontrols (Control control)
{
for (int i = control. Controls.Count-1; I >=0; i--)
{
Clearcontrols (Control. Controls[i]);
}

if (! ( Controlis TableCell))
{
if (control. GetType (). GetProperty ("SelectedItem") !=Null
{
LiteralControl literal =New LiteralControl ();
Control. PARENT.CONTROLS.ADD (literal);
Try
{
Literal. Text = (String) control. GetType (). GetProperty ("SelectedItem"). GetValue (Control,NULL);
}
Catch
{
}
Control. Parent.Controls.Remove (Control);
}
Elseif (control. GetType (). GetProperty ("Text") !=Null
{
LiteralControl literal =new literalcontrol ();
                control. PARENT.CONTROLS.ADD (literal);
                literal. text =  (string) control. GetType (). GetProperty ( "text"). GetValue (Control, null);
                control. Parent.Controls.Remove (Control);
            }
        }
        return;
    }

Problem: registerForEventValidation can only be called during render () execution; result: registerForEventValidation

can only be called during render () execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.