It is not possible to call non static method from client side using javascript / jQuery. To call the non static methos using javascript you jave some different ways like using the web service, WCF service and etc.
I am going to explaing how to call non static method with web service using javascript:
First create a new web application in my case the application name is "WebApplicationDemo".
I have added webservice in this project named as "WebService1.asmx".
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplicationDemo
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string strName)
{
return "Hello " + strName + ", The current time is = " + DateTime.Now + "";
}
}
}
Build you project and then added a new webform named as "WebForm1.aspx".
code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationDemo.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function getTime() {
var name = document.getElementById("txtName").value;
WebApplicationDemo.WebService1.HelloWorld(name, getTimeSuccess, getTimeError);
}
function getTimeSuccess(result) {
alert(result);
}
function getTimeError(error) {
alert(error);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div style="font-family: Arial">
<asp:Label runat="server" Text="Enter Your Name"></asp:Label>
<asp:TextBox runat="server" ID="txtName" Height="20" Width="150"></asp:TextBox>
<input type="button" id="btnGetTime" value="Submit" onclick="getTime()" />
</div>
</form>
</body>
</html>
Now run the project in web browser and you will get the textbox and button there in my case i have types "Purvesh" inside the textbox and press submit. It does invoke the service which contains my non static methos and get the result back.
OutPut
Hello Purvesh, The current time is = 18-Sep-15 4:04:10 PM