Communication between javascript and .Net CLR

Hi All,

I am presenting article on communication between javascript and .Net CLR. There are number of methods for the same. In this article, we will see how to communicate javascript and .Net CLR using webservice.

I have used javascript object for communication with server side method because this is a good practice to use javascript object instead of list of argument. And you can also see how javascript object will be used in communication with server side method.

Below code representing that how can we pass the information from client side to server side?

function Employee(firstName, lastName, age)
{
   Employee.prototype.firstName = firstName;
   Employee.prototype.lastName = lastName;
   Employee.prototype.age = age;
}

function insertEmployeeClick()
{
   var firstName = document.getElementById("txtFirstName").value;
   var lastName = document.getElementById("txtLastName").value;
   var age = document.getElementById("txtAge").value;

   if (firstName == "") { alert("First Name    Required");document.getElementById("txtFirstName").focus();return;}
   if (lastName == "") { alert("Last Name Required"); document.getElementById("txtLastName").focus(); return; }
   if (age == "") { alert("Age Required"); document.getElementById("txtAge").focus(); return; }

   var employee = new Employee(firstName, lastName, age);
   JSObject.InsertEmployee(employee, onInsertCallBack);
}

function onInsertCallBack(result)
{
   document.getElementById("txtFirstName").value = "";
   document.getElementById("txtLastName").value = "";
   document.getElementById("txtAge").value = "";

   if (result) { alert("Employee added successfully"); }
   else { alert("Database insert fail"); }
}

I have also attached the sample code. You can download it from here.
JSObjectAndWebService