Thursday, January 1, 2009

Accessing server side event using Ajax 3.5 with Visual Studio 2008

Accessing server side event using Ajax 3.5 in visual studio 2008.

when we compare with earlier method,This may easy to write the code for developer.

step1 :

Include in your application system.web.extensions 3.5

Step 2:

In the script manager tag EnablePageMethods attribute make it true. it will help you to create method in the pagemethod class. Then only you can access the server event in the client side.


<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
</asp:ScriptManager>

Step 3:

We should include the namespace over the server side method.[System.Web.Services.WebMethod].

Earlier we have used [Ajax.Ajaxmethod].

Example
[System.Web.Services.WebMethod]
public static string Message(string one,string two)
{
return one + two;
}

Step 4:
Client side. while calling the server side method. it will create two default methods. if it is succeed it will call OnGetMessageSuccess, and fails it will call OnGetMessageFailure.
Example

function GetCall()
{
PageMethods.Message("122","122",OnGetMessageSuccess, OnGetMessageFailure);
}
function OnGetMessageSuccess(result,userContext,methodName)
{
alert(result);
alert(userContext);
}
function OnGetMessageFailure(error,userContext,methodName)
{
alert(error.get_message());
}