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

"It is amazing how you guys have the tools that suits our needs to the "T" and I'm really glad to see that these tools are compatible with 3.5 as we would be moving there shortly."

Ramarao Prahalad
Texas Association of School Boards

Random testimonial   All testimonials