In this article I will explain with an example, how to call Server Side function without Static WebMethod using jQuery in ASP.Net using C# and VB.Net.
Whenever an AJAX call needs to be done to make call to Server Side in jQuery, one has use a Static Web Method or a Web Service Web Method.
This article will illustrate how to call the Server Side code i.e. Page Load event with JSONP (JSON with Padding) using jQuery getJSON function and CallBack functions in ASP.Net.
 
 
Server Side code
Inside the Page Load event, the value of the name QueryString parameter is checked.
If the parameter is present, a JSON string is built using the value of name QueryString parameter, Current DateTime and the callback QueryString parameter.
There JSON Generic HTTP Handler accepts two optional QueryString parameters.
Note: The callback QueryString parameter holds the value of the Callback function which will be executed by the client side script when the response is received.
 
Finally the JSON string is sent to Client through the Response.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(Request.QueryString["name"]))
    {
        //Build a JSON string.
        string responseJson = "{Name: '" + Request.QueryString["name"] + "', Time: '" + DateTime.Now + "'}";
 
        //Check for CallBack function.
        if (!string.IsNullOrEmpty(Request.QueryString["callback"]))
        {
            //Wrap a call to CallBack function with the JSON string as parameter.
            responseJson = string.Format("{0}({1});", Request.QueryString["callback"], responseJson);
        }
 
       //Send the Response in JSON format to Client.
        Response.ContentType = "text/json";
        Response.Write(responseJson);
        Response.End();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not String.IsNullOrEmpty(Request.QueryString("name")) Then
        'Build a JSON string.
        Dim responseJson As String = ("{Name: '" & Request.QueryString("name") & _
                                      "', Time: '" & DateTime.Now + "'}")
        'Check for CallBack function.
        If Not String.IsNullOrEmpty(Request.QueryString("callback")) Then
            'Wrap a call to CallBack function with the JSON string as parameter.
            responseJson = String.Format("{0}({1});", Request.QueryString("callback"), responseJson)
        End If
 
        'Send the Response in JSON format to Client.
        Response.ContentType = "text/json"
        Response.Write(responseJson)
        Response.End()
    End If
End Sub
 
 
Call Server Side function without Static Web Method using jQuery in ASP.Net
The following HTML Markup consists of an ASP.Net TextBox and a Button assigned with a jQuery click event handler.
The Server Side code (Page Load event) is called using the jQuery getJSON function. The value of the Name parameter is fetched from the TextBox while the callback parameter is passed as “?” as there’s no callback function in this scenario.
Once the response is received, the Name and the Server DateTime is displayed using JavaScript Alert Message Box.
<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="btnGet" runat="server" Text="Submit" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btnGet]").click(function () {
            var name = $("[id*=txtName]").val();
            $.getJSON('<%=ResolveUrl("~/Default.aspx")%>' + '?name=' + name + '&callback=?', function (response) {
                alert("Name: " + response.Name + "\nTime: " + response.Time);
            });
            return false;
        });
    });
</script>
</form>
 
 
Screenshot
Call Server Side function without Static Web Method using jQuery in ASP.Net
 
 
Demo
 
 
Downloads