2012-06-27 75 views
1

在這裏我打電話一個按鈕點擊一個JavaScript函數,我需要在完成其執行後調用JavaScript函數內的服務器端方法。在JavaScript函數中調用serverside方法?

JavaScript函數

function exportCharts(exportFormat) { 

      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..."; 
       } 
      } 

     } 

服務器端方法

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

,我已經嘗試過這樣的

$(document).ready(function() { 
     $("#imgBTNExportPPT").click(function (e) { 
      e.imgBTNExportPPT_Click(); 
      $.ajax({ 
       type: "POST", 
       url: "PEventPerformance.aspx/updateContent", 
       data: "{}", 
       success: function (result) { 
       } 
      }); 
     }); 
    }); 

任何建議?

+0

imgBTNExportPPT_Click是即使在按鈕上點擊#imgBTNExportPPT當你點擊按鈕時,你應該會觸發這個按鈕,而不會讓你滿意JS嗎?你的aspx代碼爲imgBTNExportPPT? – Liam

+0

您正在指定'json'的'dataType',但您調用的代碼看起來不像返回JSON數據;相反,它看起來像它提供了一個文件下載。如果是這樣的話,你可以簡單地使用'window.location.href ='PEventPerformance.aspx/updateContent';' –

+0

ya以前我曾經使用onclick ..forgot來調用那個來改變那個..現在認爲那個作爲一個函數,並告訴我如何使用ajax – nitinvertigo

回答

2

您的imgBTNExportPPT_Click看起來像一個按鈕的點擊事件。您可以嘗試以下操作,從JavaScript

將這個JavaScript

<script type="text/javascript"> 
    function myfunc() { 
     <%= Page.ClientScript.GetPostBackEventReference(imgBTNExportPPT, String.Empty) %>; 
    } 
</script> 

呼叫引發該事件在aspx頁面此功能針對的OnClientClick

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="myfunc();" /> 

這將觸發服務器端事件:

protected void imgBTNExportPPT_Click(object sender, ImageClickEventArgs e) 
{ 

} 
+0

內部JavaScript函數調用該函數,無論我是否必須像這樣給myfunc(); ??? – nitinvertigo

+0

@nitinvertigo,當然嘗試 – Habib

+0

不,它不工作.. – nitinvertigo

0

你可以使用Ajaxpro來達到這個目的,如果你想生成e服務器端調用,無需點擊按鈕等任何事件。

在您的代碼隱藏文件。根據在Page_Load部分添加

AjaxPro.Utility.RegisterTypeForAjax(typeof(YourCodebehindfilename)); 

在客戶端

調用服務器端方法類似

var content = YourCodeBehind.Yourmethod(optional parameters).value; 

在內容,你可以得到一個對象的反應,可以做進一步的修改

0

我想執行服務器端方法的最佳方法是使用Web服務。

您必須編寫一個包含您的服務器端方法的Web服務。然後您可以使用AJAX調用它。