C# 在javascript函数中调用服务器端方法?
在这里,我在点击按钮时调用一个javascript函数,我需要在javascript函数完成执行后调用其内部的服务器端方法 Javascript函数C# 在javascript函数中调用服务器端方法?,c#,jquery,asp.net,asp.net-ajax,C#,Jquery,Asp.net,Asp.net Ajax,在这里,我在点击按钮时调用一个javascript函数,我需要在javascript函数完成执行后调用其内部的服务器端方法 Javascript函数 function exportCharts(exportFormat) { initiateExport = true; for (var chartRef in FusionCharts.items) { if (FusionCharts.items[chart
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) {
}
});
});
});
任何建议???您的
imgBTNExportPPT\u单击看起来像按钮的单击事件。您可以尝试以下方法从JavaScript引发事件
将此javascript放入aspx页面
<script type="text/javascript">
function myfunc() {
<%= Page.ClientScript.GetPostBackEventReference(imgBTNExportPPT, String.Empty) %>;
}
</script>
如果您希望生成服务器端调用而不发生任何事件(如单击按钮),则可以使用Ajaxpro实现此目的
在代码隐藏文件中。在页面加载部分下添加
AjaxPro.Utility.RegisterTypeForAjax(typeof(YourCodebehindfilename));
在客户端
调用服务器端方法,如
var content = YourCodeBehind.Yourmethod(optional parameters).value;
在内容中,您可以将响应作为一个对象,并可以做进一步的更改我想执行服务器端方法的最佳方法是使用Web服务
您必须编写一个包含服务器端方法的Web服务。然后您可以使用AJAX调用它。imgBTNExportPPT\u单击是对按钮的均匀单击#imgBTNExportPPT这应该在您单击按钮而不连接JS时触发?imgBTNExportPPT的aspx代码在哪里?您正在指定json
的dataType
,但您调用的代码看起来不像返回json数据;相反,它看起来像是提供了一个要下载的文件。如果是这样,您只需执行window.location.href='PEventPerformance.aspx/updateContent'代码>以前我曾用OnCalk调用那个…忘记了改变那个…现在把它当作函数并告诉我如何调用那个ajax@nitinvertigo,我已经更新了答案,我也测试了它,它现在正在工作
AjaxPro.Utility.RegisterTypeForAjax(typeof(YourCodebehindfilename));
var content = YourCodeBehind.Yourmethod(optional parameters).value;