AJAX Page - Knowledge Base

« Back to Knowledge Base list

Feedback on JSVisible methods

Q:

One question about the client-side call of server-side function with the JSVisible attribute.
Is it possible to get success and failure feedback (response and exception) like when using PageMethods?

A:

Sure, let's do it on the cs_jsvisible.aspx(.cs) example from the Suite:

[CS]

[JSVisible]
public string ComputeAge(string name, int year)
{
  int age = DateTime.Now.Year - year;
  return name + " is " + age + " years old.";
}

[ASPX]

We can replace the following line:

    document.getElementById('divComputeAge').innerHTML = ComputeAge(name, year);

with this code:

  function callback(result, exeption) {
      if (result != null) {
          // cuccess
          document.getElementById('divComputeAge').innerHTML = result;
      } else if (exeption != null) {
          // failure
          alert("Server-side exception: " + exeption.message);
      }
  }
  ComputeAge(name, year, callback, false, true);