1

How can I call a function C# (server side) function in a Javascript function (client side) which triggers the browser is closed? as:

window.onbeforeclose = MyFunction() // C#
11
  • 4
    You'd have to send an AJAX request to the server... Commented Jul 2, 2013 at 12:28
  • are you talking about json RPC types ? Commented Jul 2, 2013 at 12:28
  • Which web framework do you use? Commented Jul 2, 2013 at 12:30
  • possible duplicate of Call ASP.NET C# Controller Method from Javascript Commented Jul 2, 2013 at 12:32
  • 1
    Another question that stems from the misunderstanding ASP.NET makes possible through it's massive abstractions (especially WebForms)... Commented Jul 2, 2013 at 12:41

2 Answers 2

4

You can not call your server side function from client side , You will need to decorate your C# method with Web Method attribute and make it static to expose it as a callable AJAX member: something like

[WebMethod]
public static void someCSharpFunction() {
 // body of function
}

and in your client side

$.ajax({
  type: "POST",
    url: "SomePage.aspx/someCSharpFunction",
   data: "{}",
   contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
 });

Hope that helps.

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

1 Comment

You can not directly call server-side functions from javascript, so you have to use javascript using Ajax extensions in Asp.Net
0

If you are looking to call a server side (c#) method from JavaScript, there are few ways to interact with server side through client side(Javascript):

  1. XMLHttpRequest
  2. Callback
  3. WebService call
  4. PageMethod

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.