Thursday, October 1, 2009

Simplest way to Call server Side funtion from Client side

Step 1: Drop a Script Manager in Page and set its EnablePageMethods property to True.

Step 2: Create a function on the server side that needs to be called from Client Side and add the attribute [WebMethod] on top of it.
[System.Web.Services.WebMethod]
public static void SaveInfo(string Id)
{
// Do server side coding……

}
Setp 3: In the Client Side function just call it like.


function makeFavorite(id) {
PageMethods.SaveInfo(id, CallSuccess, CallFailed);
}

// This will be Called on success
function CallSuccess(res, id) {
alert(destCtrl);
}

// This will be Called on failure
function CallFailed(res) {
alert(res.get_message());
}

4 comments:

Muhammad Sheraz Lodhi said...

ooh..That great.I never though this is so easy :)

Anis Ahmad said...

It saved me hours to resolve an error... Thanks for your quick and easy posts...

AfterWeb said...

not working can u give me example code if possible.

Unknown said...

@AfterWeb:

The function that you are calling from clent side should have to be static only then you can call it.

Regards,
U