Two Problems and Solutions for export the GridView in Asp.net

Source: Internet
Author: User

The code for the content exported by the GridView is roughly as follows:
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 indicates the table ID.

Note: blue marks the line of code with an error.
OK. It's hard to finish the code and run the program test. Sorry, an error is reported.
Problem 1: The "gvCompareDetail" Control of type "Grid1" must be placed in the form tag with runat = server.
Note: Grid1 is the table ID.

The solution for searching the Internet is roughly:
1) Place the Grid between <form runat = "server"> </form>.
2) Add runat = "server" to the Grid ".
Check that the front-end Grid is indeed marked with runat = "server", and the table is placed in form.
Solution: add the following Rewriting Method to the background code.
Public override void VerifyRenderingInServerForm (Control control Control)
{}
The function is used to display the Form Control for the specified ASP. NET mobile control at runtime.
Syntax:
C #
Copy codeThe Code is as follows:
Public override void VerifyRenderingInServerForm (
Control control
)

Parameters
Control
Type: System. Web. UI...:. Control
The ASP. NET mobile control must be in the Form Control.
Remarks
If the controller is not included in Form during running, this method will overwrite the Page...:. VerifyRenderingInServerForm method to cause an exception.
If the server controls for sending back or using client scripts are not included in the HtmlForm server Control (<form runat = "server">) Tag, they will not work properly. These controls can call this method during rendering to provide clear error information when they are not included in the HtmlForm control.
This method is usually called when the Render method is rewritten for any type of input tag during custom server control development. This is especially important when the input control calls GetPostBackEventReference or sends a client script. This call is not required for composite server controls.
OK, add the above functions, compile and run the debugging. Dizzy, there is another error.

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

It seems that the function added above has not completely solved the problem.
After some searches and attempts, the problem was finally solved.

Solution 1: remove the above function VerifyRenderingInServerForm. In the export code, add a Form object, a Page object, add the table, and add the Form to the Page.
The exported code is as follows:
Copy codeThe 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 Article

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.