Sys. WebForms. PageRequestManagerParserErrorException one of the solutions

Source: Internet
Author: User

With this function, you can click the button on the Web page to retrieve data from the database, write the data to the Excel file, and then download the Excel file from the server to the client.

 

The Code is as follows:

 

1. The page code is as follows:
<% @ Page Language = "C #" <br/> masterpagefile = "~ /Defaultmaster. master "<br/> autoeventwireup =" true "<br/> codefile =" p0204.aspx. CS "<br/> inherits =" page_p0204 "<br/> Title =" employee information searching "<br/> stylesheettheme =" default "%> </P> <p> <asp: content ID = "maincontent" contentplaceholderid = "samplecontent" runat = "server"> <br/> <MCE: script Language = "JavaScript" type = "text/JavaScript" src = ".. /JS/p0101.js "mce_src =" JS/p0101.js "> <! -- </P> <p> // --> </MCE: SCRIPT> </P> <p> <% -- other controls -- %> </P> <p> <Table cellspacing = "1" cellpadding = "0" border = "0 "> <br/> <tbody> <br/> <tr> <br/> <TD style =" width: 45px; Height: 570px "> </TD> <br/> <TD style =" width: 91px; height: 57px "align =" right "> <br/> <asp: updatepanel id = "uppanel_revision" runat = "server" updatemode = "Conditional"> <br/> <contenttemplate> <br/> <asp: button id = "btnoutput" runat = "server" text = "Excel download" width = "80px" Height = "20px" font-size = "9pt" onclick = "btnoutput_click"> </ASP: button> <br/> </contenttemplate> <br/> </ASP: updatepanel> <br/> </TD> <br/> </tr> <br/> </tbody> <br/> </table> </P> <p> </ASP: content>

2. The server code is as follows:

Using System; <br/> using System. data; <br/> using System. configuration; <br/> using System. collections; <br/> using System. web; <br/> using System. web. security; <br/> using System. web. UI; <br/> using System. web. UI. webControls; <br/> using System. web. UI. webControls. webParts; <br/> using System. web. UI. htmlControls; <br/> using System. IO; <br/> using System. collections. generic; <br/> using System. runtime. interop Services; <br/> using System. diagnostics; <br/> using System. drawing; <br/> // other header files <br/> // using other. header. file <br/> protected void btnOutput_Click (object sender, EventArgs e) <br/>{< br/> try <br/>{< br/> string strYear = this. lstYear. selectedValue. trim (); <br/> string strSeason = this. lstSeason. selectedValue. trim (); <br/> string strDptType = this. lstDpt. selectedValue. trim (); <br/> // grd _ Employee is a gridview that displays employee information on the page <br/> foreach (GridViewRow grdRow in this. grd_employee.Rows) <br/>{< br/> if (CheckBox) (grdRow. cells [4]). findControl ("ckb_Output ")). checked = true) <br/>{< br/> string strLocalEmpNo = (LinkButton) (grdRow. cells [1]. controls [0]). text. toString (); <br/> string strLocalEmpName = (grdRow. cells [2]). text. toString (); <br/> string strLocalEmpLevel = (grdRow. cells [3]). text. ToString (); <br/> // write to excel <br/> if (false = writeToExcel (strYear, strSeason, strLocalEmpNo, strLocalEmpLevel, strLocalEmpName, strDptType )) <br/>{< br/> // error message <br/> string msg = Messages. getMessage ("I _P0403_001", strLocalEmpNo, strLocalEmpName); <br/> SystemLog. error (Messages. getMessage ("E_P0403_001", strLocalEmpNo); <br/> ScriptManager. registerStartupScript (Page, this. getTy Pe (), String. empty, "alert ('" + msg + "');", true ); <br/>}< br/> else <br/> {<br/> string strFileName = Session ["fullName"]. toString (); <br/> FileDownload (strFileName, strLocalEmpNo, strLocalEmpName); <br/>}</p> <p> // because the FileDownload function can only download one file, so here we will handle it like this: <br/> break; <br/>}< br/> catch (System. exception ex) <br/>{< br/> // The Exception handling function defined in the actual project <br/> ExceptionHandler. handleException (ex ); <Br/>}< br/> finally <br/>{< br/> System. GC. collect (); <br/>}< br/> [DllImport ("User32.dll", CharSet = CharSet. auto)] <br/> public static extern int GetWindowThreadProcessId (IntPtr hwnd, out int ID ); <br/> /// <summary> <br/> // write the data obtained from the database to an EXCEL file. The EXCEL file format is fixed. <br/> // </summary> <br/> // <param name = "strYear"> year </param> <br/> // <param name = "strSeason"> quarter </param> <br/> // <param name = "StrEmpNo"> employee ID </param> <br/> // <param name = "strEmpLevel"> employee level </param> <br/> // <param name = "strEmpName"> employee name </param> <br/> // <param name = "strDptType"> employee Department type </param> <br/>/ // <returns> true/false </returns> <br/> public Boolean writeToExcel (string strYear, string strSeason, string strEmpNo, string strEmpLevel, string strEmpName, string strDptType) <br/>{< br/> // defines an Excel program object <br/> Excel. applicati OnClass excelApp = new Excel. applicationClass (); </p> <p> Excel. worksheet excelWorkSheet = new Excel. worksheet (); <br/> try <br/> {<br/> DataTable tblEvalutionInfo = new DataTable (); <br/> tblEvalutionInfo = _ b0204.GetEvalutionInfo (strYear, strSeason, strEmpNo); <br/> // defines a missing object <br/> object oMis = System. reflection. missing. value; <br/> // a thin object is created by the Execl program. <br/> string strEvaFormatFile = Con FigManager. getAppConfig ("InterviewFormatFileName"); <br/> string strServerPath = ConfigManager. getAppConfig ("Web. tempFile. path "); <br/> strServerPath = Server. mapPath (String. format (strServerPath, strEvaFormatFile); <br/> Excel. workbook excelWorkBook = excelApp. workbooks. open (strServerPath, <br/> oMis, oMis ); <br/> // create a thin object Worksheet <br/> excelWorkSheet = (Excel. worksheet) excelWorkBook. worksheets [1]; <br/> // set the name of the Working Table <br/> excelWorkSheet. name = strEmpName; <br/> //********************************** * <br/> // write data to EXCEL -------- start <br/> //******************** * ************** <br/> // employee ID, name <br/> string strTemp = strEmpNo + "/" + strEmpName; <br/> (Excel. range) excelWorkSheet. cells [1, 1]). set_Item (2, 3, strTemp); </p> <p> // tblE Data in valutionInfo is written to other Cells in EXCEL. </p> <p> //******************************* ***** <br/> // write data to EXCEL -------- end <br/> //***************** * **************** <br/> string strTempFileName = Session. sessionID + System. dateTime. now. toString ("yyyyMMdd_HHmmss_fffff") + ". xls "; <br/> string address = @ ConfigManager. getAppConfig ("Web. temp. path ") + strTempFileName; <br/> // Save the temporary file name in the Session and delete the temporary files when the Session ends <br/> Session ["fullName"] = Server. mapPath (address ). toString (); <br/> excelWorkSheet. saveAs (Server. mapPath (address), <br/> oMis, oMis); <br/> if (excelWorkSheet! = Null) <br/>{< br/> System. Runtime. InteropServices. Marshal. ReleaseComObject (excelWorkSheet); <br/>}< br/> if (excelWorkBook! = Null) <br/>{< br/> System. Runtime. InteropServices. Marshal. ReleaseComObject (excelWorkBook); <br/>}< br/> if (excelApp! = Null) <br/>{< br/> excelApp. workbooks. close (); <br/> excelApp. quit (); <br/> System. runtime. interopServices. marshal. releaseComObject (excelApp); <br/>}< br/> excelWorkSheet = null; <br/> excelWorkBook = null; <br/> excelApp = null; <br/>}< br/> catch (Exception ex) <br/>{< br/> // The Exception handling function defined in the actual project <br/> ExceptionHandler. handleException (ex); <br/> // close excel file <br/> if (excelWorkSheet! = Null) <br/>{< br/> System. Runtime. InteropServices. Marshal. ReleaseComObject (excelWorkSheet); <br/>}< br/> if (excelApp! = Null) <br/>{< br/> excelApp. workbooks. close (); <br/> excelApp. quit (); <br/> System. runtime. interopServices. marshal. releaseComObject (excelApp); <br/>}< br/> return false; <br/>}< br/> return true; <br/>}< br/> /// <summary> <br/> // download the file from the server to the client <br/> /// </summary> <br/> // <param name = "FullFileName"> full path of the file to be downloaded on the server </param> <br/> // <param name =" strEmpNo "> employee ID </param> <br/> // <param name =" strEmpName "> employee name </param> <br/> // <returns> none </returns> <br/> protected void FileDownload (string FullFileName, string strEmpNo, string strEmpName) <br/>{< br/> // file name when downloading to the client. <br/> string fileName = String. format (Evaluation. fw. constants. businessConstants. formatInterviewFileName, <br/> this. lstYear. selectedValue. trim (), this. lstSeason. selectedValue. trim (), <br/> strEmpName, strEmpNo); <br/> System. IO. fileInfo fileInfo = new System. IO. fileInfo (FullFileName); <br/> if (fileInfo. exists = true) <br/>{< br/> const long ChunkSize = 102400; <br/> byte [] buffer = new byte [ChunkSize]; <br/> Response. clear (); <br/> System. IO. fileStream iStream = System. IO. file. openRead (FullFileName); <br/> long dataLengthToRead = iStream. length; <br/> Response. contentType = "application/octet-stream"; <br/> Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName); <br/> while (dataLengthToRead> 0 & Response. isClientConnected) <br/>{< br/> int lengthRead = iStream. read (buffer, 0, Convert. toInt32 (ChunkSize); <br/> Response. outputStream. write (buffer, 0, lengthRead); <br/> Response. flush (); <br/> dataLengthToRead = dataLengthToRead-lengthRead; <br/>}< br/> Response. close (); <br/>}< br/>}

 

In this case, the exception Sys. WebForms. PageRequestManagerParserErrorException is generated, as follows:

 

The specific cause is unclear, and then compare other similar functions in this project, it is found that the EXCEL Download (ID: btnOutput) button on the page is in an UpdatePanel, this exception occurs, debugging found, this exception occurs when a temporary file is generated on the server but is not downloaded to the client, so you can find the cause on the page and the download function. the reason for this exception is that the level is limited and has not been investigated yet. however, if you remove UpdatePanel, You can implement the required functions.

The modified page code is as follows:

<% @ Page Language = "C #" <br/> MasterPageFile = "~ /DefaultMaster. master "<br/> AutoEventWireup =" true "<br/> CodeFile =" P0204.aspx. cs "<br/> Inherits =" Page_P0204 "<br/> Title =" Employee Information Searching "<br/> StylesheetTheme =" Default "%> </p> <p> <asp: content ID = "mainContent" ContentPlaceHolderID = "SampleContent" runat = "Server"> <br/> <mce: script language = "javascript" type = "text/javascript" src = ".. /Js/P0101.js "mce_src =" Js/P0101.js "> <! -- </P> <p> // --> </mce: script> </p> <% -- other controls -- %> </p> <table> <br/> <tr> <br/> <td class = "" align = "right" style = "width: 360px "> <br/> <asp: button ID = "btnOutput" runat = "server" Text = "EXCEL Download" CssClass = "commonbutton" Width = "90px" OnClick = "btnOutput_Click"/> <br/> </ td> <br/> </tr> <br/> </table> </p> </asp: content>

 

In this way, when you click the EXCEL Download button (with the ID btnOutput), you can Download the file from the server.

 

 

 

 

 

 

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.