After the Excel file is exported, the page button becomes invalid (the page is suspended)

Source: Internet
Author: User

Add the following code to page_load:

String beforesubmitjs = "\ nvar exportrequested = false; \ n ";
Beforesubmitjs + = "Var beforeformsubmitfunction = theform. onsubmit; \ n ";
Beforesubmitjs + = "theform. onsubmit = function () {\ n ";
Beforesubmitjs + = "Var returnval = beforeformsubmitfunction (); \ n ";
Beforesubmitjs + = "If (exportrequested & returnval) {_ spformonsubmitcalled = false; exportrequested = false;} \ n ";
Beforesubmitjs + = "Return returnval; \ n ";
Beforesubmitjs + = "}; \ n ";
This. Page. clientscript. registerstartupscript (this. GetType (), "alterformsubmitevent", beforesubmitjs, true );

Register the Javascript program, and then send a parameter when the button is submitted:

This. _ btnsubmit. attributes ["onclick"] = "javascript: exportrequested = true ;";

This can solve this problem.

Principle: When the page button is submitted, a built-in method will be called and a built-in Variable _ spformonsubmitcalled will be modified. We can call the button to reset the method.

When the value of this parameter reaches the system retrieval page, it is always in the unsubmitted status by default.

==================== Note: ======================

However, if you click the export button on the ascx control instead of clicking the button on the ASPX page, the above method will report an error,

. _ Btnsubmit is the button for exporting an Excel file;
Registerclientscriptblock registers the code after the source file. If registerstartupscript is not declared by theform. onsubmit, an error is reported.

 

==================== Note: ======================

It is normal to select "open" for the generated Excel file;
Select "save". All scripts on the page are invalid. The previous question must also be "target is _ Self ".
The error message is "Unknown error".
My personal analysis thinks that this is the generated page. Although it is opened in the pop-up, the set setheader is already in the Excel file format by default. This leads to the way IE calls JS (speculation ), now the solution is to set the target to _ blank, but the result is an additional white page.

==================== Note: ======================

Protected void page_load (Object sender, eventargs E)
{
Btnexcel = (button) findcontrol ("btnexcel ");

# Region [the page button becomes invalid after using response to generate EXCEL or word in Moss. solution]
String beforesubmitjs = "/nvar exportrequested = false;/N ";
Beforesubmitjs + = "Var beforeformsubmitfunction = theform. onsubmit;/N ";
Beforesubmitjs + = "theform. onsubmit = function () {/N ";
Beforesubmitjs + = "Var returnval = beforeformsubmitfunction ();/N ";
Beforesubmitjs + = "If (exportrequested & returnval) {_ spformonsubmitcalled = false; exportrequested = false;}/N ";
Beforesubmitjs + = "Return returnval;/N ";
Beforesubmitjs + = "};/N ";
This. Page. clientscript. registerstartupscript (this. GetType (), "alterformsubmitevent", beforesubmitjs, true );
This. btnexcel. attributes ["onclick"] = "javascript: exportrequested = true ;";
# Endregion

If (! Page. ispostback)
{
Page. databind ();
}
}

 

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.