1

Here i am calling a javascript function on a button click and i need to call the server side method inside the javascript function after finishing its execution.

Javascript Function

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

        }

Server side Method

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

and i have tried like this

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

Any suggestion??

3
  • imgBTNExportPPT_Click is the click even on the button #imgBTNExportPPT this should get fired when you click the button without you wirting JS? where your aspx code for imgBTNExportPPT? Commented Jun 27, 2012 at 10:02
  • You're specifying a dataType of json, but the code you're calling doesn't look like it returns JSON data; instead it looks like it serves up a file to be downloaded. If that is the case, you can simply do window.location.href = 'PEventPerformance.aspx/updateContent'; Commented Jun 27, 2012 at 10:04
  • ya previously i used to call that one using onclick..forgot to change that one..now consider that one as a function and tell me how to call that one using ajax Commented Jun 27, 2012 at 10:04

3 Answers 3

2

Your imgBTNExportPPT_Click looks like an click event of a button. You may try the following to raise the event from JavaScript

Place this javascript in aspx page

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

Call this function against OnClientClick

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

This will fire the server side event:

protected void imgBTNExportPPT_Click(object sender, ImageClickEventArgs e)
{

}
Sign up to request clarification or add additional context in comments.

1 Comment

@nitinvertigo, I have updated the answer and I have also tested it, it is working now
0

You can use Ajaxpro for this purpose, If u want to generate a server side call without any event like button click.

In Your code behind file. Under the Page_Load section add

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

In client side

call the server side method like

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

In content you can get your response as an object and can do further changes

Comments

0

I guess the best way to execute server side method is to use Web Services.

You have to write a Web Service that that contains your server side method.Then you can call it using AJAX.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.