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 ();
}
}