Two problems encountered in the GridView export in asp.net and their solutions _ practical skills

Source: Internet
Author: User
The code for the content that the GridView exports 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 is the ID of the table

Note: Blue identifies the code as the line of error.
OK, finally knock the code, run the program test. Pedal, the error.
problem One: the control "Gvcomparedetail" of type "GRID1" must be placed inside a form tag with Runat=server.
Note: GRID1 is the ID of the table.

Find the solution on the internet roughly as follows:
1) Put the grid into the <form runat= "server" ></form> room.
2) to the grid tagged runat= "server."
Find out that the front grid does have tagged runat= "server" and that the form is placed in the form.
Workaround: Add the following override method in the background code
public override void Verifyrenderinginserverform (Control control)
{ }
Look for the MSDN description, which is for the purpose of confirming that the Form control is rendered for the specified ASP.net mobile control at run time.
Grammar:
C#
Copy Code code as follows:

public override void Verifyrenderinginserverform (
Control control
)

Parameters
Control
Type: System.Web.UI.::. Control
The ASP.net mobile control must be located in the Form control.
Note
If the control is not included in the Form at run time, this method overrides Page..::. Verifyrenderinginserverform method to throw an exception.
server controls that are postback or that use client script are not included in the HtmlForm server control (<form runat= "Server" >) tags, and they will not work correctly. These controls can call this method at render time to provide a clear error message when they are not included in the HtmlForm control.
When you develop a custom server control, you typically call this method when you override the Render method for any type of input token. This is especially important when the input control invokes GetPostBackEventReference or emits client script. The composite server control does not need to make this call.
OK, plus the above function, compile and run debugging. Halo, another appearance of another error.

Problem two: registerForEventValidation can only be invoked during the execution of Render ().

It seems that the above function does not completely solve the problem.
After a search and try, finally solved the problem.

Workaround 1: Remove the above function verifyrenderinginserverform, in the export code, dynamically add a Form object, a Page object, add the table to it, and adds the form to page.
The exported code is as follows:
Copy Code code 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 ();

Workaround 2: Modify Web.config (not recommended) <pages enableEventValidation = "false" ></pages>

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.