1
Reply

What is the best way to call a C# method from Javascript

Anand N

Anand N

11y
1.8k
0
Reply

    Here i am giving the way by using you can call c# method in javascript In .aspx page create a function something like the show n below function InsertImp(id) { var v1 = 'Id:' + id ; $.ajax( { type: "POST", url: '<%= ResolveUrl("~/Demo.aspx/Demo") %>', data: '{' + v1 + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { if (result.status === "OK") { alert('Comment posted'); } else { return false; } }, error: function (req, status, error) { alert("Sorry! Post failed due to error"); } }); } and in your aspx.cs page write in this way (the method which you want to call from javascript function) public partial class Demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static int demo(int Id) { return 0; } } In this way you can able to call the C# method from a javascript function. For learning similar kind of questions you can refer this link c# interview questions