强制一个函数在单击按钮之前的另一个函数之前完成执行

时间:2012-06-27 14:10:19

标签: c# javascript buttonclick

这里我试图执行两个函数,即单击一个按钮就可以执行一个javascript和一个c#代码。问题是函数是同时执行的。这是我的函数:

Javascript功能

 function exportCharts() {
            var exportFormat = 'JPG';
            initiateExport = true;
            for (var chartRef in FusionCharts.items) {
                if (FusionCharts.items[chartRef].exportChart) {
                    document.getElementById("linkToExportedFile").innerHTML = "Exporting...";
                    FusionCharts.items[chartRef].exportChart({ "exportFormat": exportFormat });
                }
                else {

                    document.getElementById("linkToExportedFile").innerHTML = "Please wait till the chart completes rendering...";
                }
            }
        }

c#c​​ode:

 protected void imgBTNExportPPT_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                PredictExportToPPT objOExporttoPPT = new PredictExportToPPT();

                PredictionModel();

                string reportNames = ObjCommon.GetBIReportNames("Prediction", "Report");
                reportNames += ObjCommon.GetBIReportNames("Prediction", "Table");

                objOExporttoPPT.ExportToPPTPredict(ObjPredictInputParameter, reportNames, ObjSharedEntities.PredictTableData);

                string itemname = "PPTOutput.pptx";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "pptx";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + itemname + "");
                HttpContext.Current.Response.BinaryWrite(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(DataTemplate.PPTOutputTemplateFilePath)));
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();

            }
            catch (Exception exceptionMessage)
            {
                throw (exceptionMessage);
            }
            finally
            {
                GC.Collect();
            }
        }

按钮点击代码

 <asp:ImageButton ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0" OnClientClick="return exportCharts()" OnClick="imgBTNExportPPT_Click" />

如果我尝试使用 System.Threading.Thread.Sleep 作为c#代码,javascript代码首先执行但不是完全执行。它等待执行c#代码,最后两个输出同时生成。我希望它一个接一个地发生。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

我测试过它正在运行

<asp:ImageButton ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0" OnClientClick="exportReport()" OnClick="imgBTNExportPPT_Click" />

   <script>
            function exportReport() {
              exportCharts();
             __doPostBack("imgBTNExportPPT","");
         }
    </script>

如果您使用的是母版页,那么:

替换

__doPostBack("imgBTNExportPPT","");

__doPostBack("ctl00$MainContent$imgBTNExportPPT", "");