ASP. NET's gridview exports the Excel statement in updatepanel

Source: Internet
Author: User
Microsoft Ajax technology is used in ASP. NET to export the Excel file in updatepanel.
In the ASP. net2.0 framework, when you use the gridview to export execl, an error message indicating that registerforeventvalidation can only be called during render () execution will occur.

Method 1: (it does not seem to work)
Response. Write conflicts in Ajax. You must add the <triggers> attribute to updatepanel and point the controlid to the exported ID.
<Asp: button id = "button3" runat = "server" text = "Export excel" onclick = "button3_click"/>
</Contenttemplate>
<Triggers>
<Asp: postbacktrigger controlid = "button3"> </ASP: postbacktrigger>
</Triggers>

CS:
Protected void button3_click (Object sender, eventargs E)
{
Export ("application/MS-excel", "product information table .xls ");
}
Private void Export (string filetype, string filename)
{
Gridview1.enableviewstate = false;
Response. charset = "gb2312 ";
Response. contentencoding = system. Text. encoding. utf7;
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, encoding. utf8). tostring ());
Response. contenttype = filetype;
This. enableviewstate = false;
Stringwriter Tw = new stringwriter ();
Htmltextwriter hW = new htmltextwriter (TW );
Gridview1.rendercontrol (HW );
Response. Write (TW. tostring ());
Response. End ();
}
Method 2: (yes)
Response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
Response. appendheader ("content-disposition", "attachment?filename=filename.xls ");
// If it is set to getencoding ("gb2312"), the exported file will contain garbled characters !!!
Response. contentencoding = system. Text. encoding. utf7;
Response. contenttype = "application/MS-excel"; // set the output file type to an Excel file.
System. Io. stringwriter ostringwriter = new system. Io. stringwriter ();
System. Web. UI. htmltextwriter ohtmltextwriter = new system. Web. UI. htmltextwriter (ostringwriter );
This. gridview1.rendercontrol (ohtmltextwriter );
Response. Output. Write (ostringwriter. tostring ());
Response. Flush ();
Response. End ();
}

}
// This function must be reloaded.
Public override void verifyrenderinginserverform (Control)
{}

Method 4: (garbled characters may occur)
Public void createexcel (Dataset ds, string typeid, string filename)
{
Httpresponse resp;
Resp = page. response;
Resp. contentencoding = system. Text. encoding. getencoding ("UTF-8 ");
Resp. appendheader ("content-disposition", "attachment; filename =" + filename );
String colheaders = "", ls_item = "";
Int I = 0;

//
Datatable dt = Ds. Tables [0];
Datarow [] myrow = DT. Select ("");
// Typeid = "1" is an Excel file; typeid = "2" is an XML file
If (typeid = "1 ")
{
//
For (I = 0; I <DT. Columns. Count; I ++)
{
If (I = DT. Columns. Count-1)
{
Colheaders + = DT. Columns [I]. Caption. tostring () + "\ n ";
}
Else
{
Colheaders + = DT. Columns [I]. Caption. tostring () + "\ t ";
}
}
//
Resp. Write (colheaders );
//
Foreach (datarow row in myrow)
{
//
For (I = 0; I <DT. Columns. Count; I ++)
{
If (I = DT. Columns. Count-1)
{
Ls_item + = row [I]. tostring () + "\ n ";
}
Else
{
Ls_item + = row [I]. tostring () + "\ t ";
}
}
//
Resp. Write (ls_item );
Ls_item = "";
}
}
Else
{
If (typeid = "2 ")
{
// Export XML data directly from dataset and write it to the HTTP output stream
Resp. Write (Ds. getxml ());
}
}
// Write the data in the buffer to the HTTP header file
Resp. End ();
}
Call createexcel (DS, "1", "filename.xls ");
There are three ways to solve the above problems:
1. modify web. config (not recommended) <pages enableeventvalidation = "false"> </pages>
2. Modify it directly on the export execl page
<% @ Page Language = "C #" enableeventvalidation = "false" autoeventwireup = "true" codefile = "exportgridview. aspx. cs" inherits = "exportgridview" %>
3. Use scriptmanager. registerclientscriptblock () and other methods

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.