A total of four solutions are collected. I don't know which solutions are applicable to your project:
1 Add the following in page_load:Code:
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 JavascriptProgramAnd then send a parameter when the button is submitted:
This. _ btnsubmit. attributes ["onclick"] = "javascript: exportrequested = true ;";
there are two problems to use this method: (1) if your export button is written in usercontrol, the above method will report an error because registerclientscriptblock registers the code to the back of the source file, if registerstartupscript is used, theform. if onsubmit is not declared, an error is returned. (2) If you select "open" for the generated Excel file, it is normal. If you select "save", all scripts on the page become invalid, the error message "Unknown error" must also be returned when the target is _ self. 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 extra white page.
2 (1) Add the onclientclick event to the original export button.
btnexport. onclientclick = "Return autoform_export ('postbackeventtarget _ isexport');";
(2) Add the JavaScript function autoform_export to the client.
function autoform_export (postbackeventtarget)
{< br> webform_dopostbackwithoptions (New webform_postbackoptions (postbackeventtarget, "", true, ", false, true ));
return false;
}< br> (3) the server determines whether the event is an export event based on request ["_ eventtarget. Note the following red font comments
If (page. request ["_ eventtarget"]! = NULL)
{
Bool isinasyncpostback = false;
Scriptmanager Sm = scriptmanager. getcurrent (PAGE );
If (SM! = NULL)
{
Isinasyncpostback = Sm. isinasyncpostback;
}
If (isinasyncpostback)
{
/*
Make sure to determine whether axaj is used for asynchronous requests. Otherwise, after you click the export button, other Ajax requests, page. request ["_ eventtarget"] retains the original value, which leads to incorrect judgment.
*/
_ Isexport = false
}
Else
{
String postbackeventtarget = page. request ["_ eventtarget"]. tostring ();
If (postbackeventtarget = "postbackeventtarget_isexport ")
{
_ Isexport = true;
}
Else
_ Isexport = false;
}
}
Else
_ Isexport = false;
}
// I don't know what the "_ isexport" variable is
3. This method is simple and clear, but it has its limitations and can only be used in button events on the server side (I personally think it is applicable to server controls with onclientclick events) add a client event to all the buttons: onclientclick = "_ spformonsubmitcalled = false ;".
4. register two lines of Javascript script in page_load. String script = "_ sporiginalformaction = Document. Forms [0]. Action; \ n_spsuppressformonsubmitwrapper = true ;";
This. clientscript. registerclientscriptblock (this. GetType (), "script", script, true );
It may be affected by other factors. Only the fourth method works. We look forward to your verification for other methods!