Calling a C# method from client side

Posted by Laghaterohan under ASP.NET AJAX on 5/7/2013 | Points: 10 | Views : 4248 | Status : [Member] | Replies : 3
Hello,
Can you please help me in how to call a C# method from client side?
I have a c# method

[Web Method]
public string MyMethod ( string name) {
// code here
}

I need to invoke/ call this method 'MyMethod' from client side using Jquery / Ajax .

How to accomplish this?

Thanks

Best Regards,
Rohan Laghate



Responses

Posted by: Learningtorise on: 5/7/2013 [Member] Starter | Points: 25

Up
0
Down
$(document).ready(function () {

$.ajax({
type: "POST",
url: "PageNAME.aspx/MyMethod",
data: "{name:'" + NAME-VALUE + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do Something Here
}, //success
error: function (msg, status) {
if (status === "error") {
var errorMessage = $.parseJSON(msg.responseText);
alert(errorMessage.Message);
}
}
}); // ajax

}); //document ready

http://hashtagakash.wordpress.com/

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: harikrishna149-10896 on: 5/7/2013 [Member] Starter | Points: 25

Up
0
Down
See the following code.

Default.aspx:
---------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<title></title>
<script type="text/javascript">
function GetDateTime() {
var params = "{'msg':'From Client'}";
$.ajax
({
type: "POST",
url: "Default.aspx/GetServerDateTime",
data: params,
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
// myString = result.d;
alert(result.d);
},
error: function (err) {

}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="GetMessage" type="button" value="Get Messages" onclick="GetDateTime();"/>
</div>
</form>
</body>
</html>


Default.aspx.cs
-----------------------------------------
using System;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page
{

[WebMethod]
public static string GetServerDateTime(string msg)
{
String result="Result : " + DateTime.Now.ToString() + " - From Server";
return result;
}
}




Thanks & Regards
Hari Krishna

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 5/7/2013 [Member] Starter | Points: 25

Up
0
Down
@Harikrishna.149@Gmail.Com : Nice one... You didn't mentioned parameter in ur ajax function though...

Change your UserName na.... I think for Privacy purpose, Ur EmailID should not be ur UserName...

:)

http://hashtagakash.wordpress.com/

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response