Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Calling Page WebMethod from JavaScript

$
0
0

i'm trying to call a page webmethod from another page on page close event ...

nothing seems to work & i found out that onunload & onbeforeunload events are not supported on chrome & opera ..

& when i try to test on FireFox i detect that it enters the event fine but don't call the Page WebMethod at all ..

here's my method

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod]
public static void CallMethod()
{
   Journals Test = new Journals();
   Test.LoadAccountData();
}
public void LoadAccountData()
{
   try
   {
     if (Session["AccountsDetails"] != null)
     {
        DataRow AccountData = Session["AccountsDetails"] as DataRow;
        txtAccountID.Text = AccountData["AccountID"].ToString();
        txtAccountName.Text = AccountData["AccountName"].ToString();
        bool AccountType = Convert.ToBoolean(AccountData["isDebt"].ToString());
        if (AccountType)
           rbisDebt.Checked = true;
        else
           rbisCredit.Checked = true;
      }
      else
        divErrorMessage.InnerHtml = GenerateErrorMessage("Account Details Was Not Loaded Correctly ... ");
   }
   catch (Exception ex)
   {
      divErrorMessage.InnerHtml = GenerateErrorMessage(ex.Message);
   }
}

& i tried 2 versions of scripts  .. this one .. 

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager><script type="text/javascript">
    window.onbeforeunload = function (ev) {
       var e = ev || window.event;
       alert("in onbeforeunload event");$.ajax({
          type: "POST",
          url: "Journals.aspx/CallMethod",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: OnSuccess,
          failure: function (response) {
                      alert(response.d);
                   }
       });
     var Message = "Page Closed...";
     if (e) {
        // returning messages for FireFox
        e.returnValue = Message;
     }
     //returning messages for Chrome
     return Message;
  };</script>

& this one

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager><script type="text/javascript">
    window.onbeforeunload = function (ev) {
       var e = ev || window.event;
       alert("in onbeforeunload event");
       PageMethods.CallMethod();
       var Message = "Page Closed...";
       if (e) {
          // returning messages for FireFox
          e.returnValue = Message;
       }
       //returning messages for Chrome
       return Message;
     };</script>

but nothing seems to happen at all .. my other page is not updated as it should be ...

i'm using VisualStudio 2012 , .NET4.5

& i have noticed one more thing .. while writing the script .. on VS it should detect the key words like PageMethods & $.ajax but auto complete shows nothing like these are not defined at all ..,

any thoughts ?




Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>