Two problems and solutions encountered with the GridView export in Asp.net

  • 2020-05-07 19:27:19
  • OfStack

The code for the GridView export looks like this:
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
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);
this.grid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
//grid1 is ID of the form

Note: blue marks the line of code where the error occurred.
OK, finally finished typing the code, run the program test. Push off. Wrong.
Problem 1: the control "gvCompareDetail" of type "Grid1" must be placed in a form tag with runat=server.
Note :Grid1 is ID of the form.

Find online solution is roughly:
1) put Grid in < form runat="server" > < /form > Between.
2) mark runat="server".
Make sure that the foreground Grid is indeed marked runat="server" and that the form is in form.
Solution: add the following rewrite method to the code in the background
public override void VerifyRenderingInServerForm(Control control)
{ }
Find MSDN instructions. This function verifies that the Form control is rendered at run time for the specified ASP.NET mobile control.
Grammar:
C#
 
public override void VerifyRenderingInServerForm( 
    Control control 
) 

parameter
control
Type: System. Web. UI.. : : Control
The ASP.NET mobile control must be in the Form control.
note
If the control is not included in Form at run time, this method overrides Page.. ::.VerifyRenderingInServerForm method to throw an exception.
If the server control using the postback or client script is not included in the HtmlForm server control ( < form runat="server" > ) in the tag, they will not work properly. These controls can call this method at render time to provide explicit error information if they are not included in the HtmlForm control.
When developing a custom server control, the Render method is typically called when it is overridden for any type of input token. This is especially important when the input control invokes GetPostBackEventReference or emits a client script. The composite server control does not need to make this call.
OK, plus the above functions, compile run debugging. Dizzy, appeared another outside 1 error.

Problem 2: RegisterForEventValidation can only be called during Render().

It doesn't look like the function that I added to this is completely solving the problem.
After a lot of searching and trying, the problem was finally solved.

Solution 1: remove the above function VerifyRenderingInServerForm, in the export code, dynamically add 1 Form object, 1 Page object, add the table to it, and add Form to Page.
The exported code is as follows:
 
Page p=new Page(); 
HtmlForm form=new HtmlForm(); 
Grid1.EnableViewState = false; 
p.EnableEventValidation = false; 
p.DesignerInitialize(); 
form.Controls.Add(Grid1); 
p.Controls.Add(form); 
StringBuilder sb=new StringBuilder(); 
StringWriter sw=new StringWriter(sb); 
p.RenderControl(sw); 
Response.Clear(); 
Response.Buffer = true; 
Response.ContentType = "application/vnd.ms-excel"; 
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); 
Response.Charset = "UTF-8"; 
Response.ContentEncoding = Encoding.Default; 
Response.Write(sb.ToString()); 
Response.End(); 

Solution 2: modify web.config(not recommended) < pages enableEventValidation ="false" > < /pages >

Related articles: