title: forcing one function to complete execution before another function on button click
body: <p>Here i am trying to execute two functions i.e. one javascript and one c# code on a single button click.The problem is both the functions are executing simultaneously.Here are my functions:</p>
  
  <p>Javascript function</p>
  
  <pre><code> 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...";
                  }
              }
          }
  </code></pre>
  
  <p>c# code:</p>
  
  <pre><code> 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();
              }
          }
  </code></pre>
  
  <p>Button click code</p>
  
  <pre><code> &lt;asp:ImageButton ID="imgBTNExportPPT" runat="server" Width="15" Height="15" border="0" OnClientClick="return exportCharts()" OnClick="imgBTNExportPPT_Click" /&gt;
  </code></pre>
  
  <p>If i try to use <strong>System.Threading.Thread.Sleep</strong> for the c# code,the javascript code is executing first but not completely.It waits till the execution of c# code and finally both the outputs are generated simultaneously.I want it to happen one after the other. Any suggestions?</p>