The code for the content exported by GridView is roughly as follows:
();
= true;
= "GB2312";
("Content-Disposition", "attachment;filename=" + fileName + ".xls");
= ("GB2312");
= "application/ms-excel";
= false;
myCItrad = new ("ZH-CN", true);
oStringWriter = new (myCItrad);
oHtmlTextWriter = new (oStringWriter);
this.(oHtmlTextWriter);
(());
();
//grid1 is the ID of the table
Note: The blue identification code is the line of code that has an error.
OK, I finally finished typing the code and ran the program test. I pedaled, I reported an error.
Question 1: The control "gvCompareDetail" of type "Grid1" must be placed in the form tag with runat=server.
Note: Grid1 is the ID of the table.
The solution to search online is roughly:
1) Put the Grid in <form runat="server"></form>.
2) Mark the Grid to runat="server".
It was found that the front desk Grid was indeed marked runat="server", and the table was placed in the form.
Solution: Add the following rewrite method in the background code
public override void VerifyRenderingInServerForm(Control control)
{ }
Find the MSDN description. The function of this function is to confirm that the Form control is rendered for the specified mobile control at runtime.
grammar:
C#
public override void VerifyRenderingInServerForm(
Control control
)
parameter
control
Type:..::.Control
The move control must be located in the Form control.
Remark
If the control is not included in the Form at runtime, this method overrides the Page..::.VerifyRenderingInServerForm method to throw an exception.
If the server controls that post back or use client scripts are not included in the HtmlForm server control (<form runat="server">) tag, they will not work properly. These controls can call the method when rendered to provide explicit error information when they are not included in the HtmlForm control.
When developing custom server controls, it is usually called when rewriting the Render method for any type of input markup. This is especially important when the input control calls GetPostBackEventReference or emits a client script. The composite server control does not need to make this call.
OK, add the above functions to compile and run debugging. I was dizzy, and another error occurred.
Question 2: RegisterForEventValidation can only be called during the execution of Render().
It seems that the function added above does not completely solve the problem.
After some search and attempt, the problem was finally solved.
Solution 1: Remove the above function VerifyRenderingInServerForm. In the export code, dynamically add a Form object and a Page object, add the table to it, and add the Form to Page.
The exported code is as follows:
Page p=new Page();
HtmlForm form=new HtmlForm();
= false;
= false;
();
(Grid1);
(form);
StringBuilder sb=new StringBuilder();
StringWriter sw=new StringWriter(sb);
(sw);
();
= true;
= "application/-excel";
("Content-Disposition", "attachment;filename=" + fileName + ".xls");
= "UTF-8";
= ;
(());
();
Solution 2: Modify (not recommended)<pages enableEventValidation ="false" ></pages>
();
= true;
= "GB2312";
("Content-Disposition", "attachment;filename=" + fileName + ".xls");
= ("GB2312");
= "application/ms-excel";
= false;
myCItrad = new ("ZH-CN", true);
oStringWriter = new (myCItrad);
oHtmlTextWriter = new (oStringWriter);
this.(oHtmlTextWriter);
(());
();
//grid1 is the ID of the table
Note: The blue identification code is the line of code that has an error.
OK, I finally finished typing the code and ran the program test. I pedaled, I reported an error.
Question 1: The control "gvCompareDetail" of type "Grid1" must be placed in the form tag with runat=server.
Note: Grid1 is the ID of the table.
The solution to search online is roughly:
1) Put the Grid in <form runat="server"></form>.
2) Mark the Grid to runat="server".
It was found that the front desk Grid was indeed marked runat="server", and the table was placed in the form.
Solution: Add the following rewrite method in the background code
public override void VerifyRenderingInServerForm(Control control)
{ }
Find the MSDN description. The function of this function is to confirm that the Form control is rendered for the specified mobile control at runtime.
grammar:
C#
Copy the codeThe code is as follows:
public override void VerifyRenderingInServerForm(
Control control
)
parameter
control
Type:..::.Control
The move control must be located in the Form control.
Remark
If the control is not included in the Form at runtime, this method overrides the Page..::.VerifyRenderingInServerForm method to throw an exception.
If the server controls that post back or use client scripts are not included in the HtmlForm server control (<form runat="server">) tag, they will not work properly. These controls can call the method when rendered to provide explicit error information when they are not included in the HtmlForm control.
When developing custom server controls, it is usually called when rewriting the Render method for any type of input markup. This is especially important when the input control calls GetPostBackEventReference or emits a client script. The composite server control does not need to make this call.
OK, add the above functions to compile and run debugging. I was dizzy, and another error occurred.
Question 2: RegisterForEventValidation can only be called during the execution of Render().
It seems that the function added above does not completely solve the problem.
After some search and attempt, the problem was finally solved.
Solution 1: Remove the above function VerifyRenderingInServerForm. In the export code, dynamically add a Form object and a Page object, add the table to it, and add the Form to Page.
The exported code is as follows:
Copy the codeThe code is as follows:
Page p=new Page();
HtmlForm form=new HtmlForm();
= false;
= false;
();
(Grid1);
(form);
StringBuilder sb=new StringBuilder();
StringWriter sw=new StringWriter(sb);
(sw);
();
= true;
= "application/-excel";
("Content-Disposition", "attachment;filename=" + fileName + ".xls");
= "UTF-8";
= ;
(());
();
Solution 2: Modify (not recommended)<pages enableEventValidation ="false" ></pages>