May 19, 2015

Access C# Method Using JQuery

In This Article I Will Explain You Guys About Accessing Method In C# From JQuery.
My Example Going To Be Very Simple. I Will Have Web Form To Enter A Name & Button Saying "Say Hello".
If You Click The Button Alert Should Come Saying "Hello <Name>".
First Of All Make Sure You Have Simple Idea About ASP.NET Web Sites, C# and JQuery.

Lets Get Into Works.


Create A New "ASP.NET Empty Web Site" & Add A New Web Form For That.

In My Case I have Created As Below.


Then Add A JQuery File To The Web Site.
Then Create The Text Field and Button As Below.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="jquery.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="text" id="txtName" />
            <br/>
            <input type="button" id="btnSayHello" value="Say Hello"/>
        </div>
    </form>
</body>
</html>

Now Lets Create The C# Method.
First Of All Before Sending A Parameter Lets Start With Simple Ajax Call.
Go To "Default.aspx.cs" File & Create Method As Below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string sayHello()
    {
        return "Hello Test";
    }

}

Try To Understand That, Say Hello Is Not Just A Simple Method In C#.It's A Web Method.
Now We Have Created The C# Method.Now What We Have To Do Is Call This Method Using AJAX.

In Order To Do That I'm Creating A Method In Java Script Section.
I Have  Added Script Tag Just Below The Head Tag & I Have Created Method Called "SayHello". Inside This Method I'm Calling The C# Method Using Ajax Call. 

<script>
        function sayHello() {
            $.ajax({
                type: "POST",
                url: "Default.aspx/sayHello",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert(response.d);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        }

</script>

Run The Project And See.




Now Lets Call The Function With Entered Value.To Do This We Should Call The Ajax Function With JSON Parameter & Change Web Method To Access A Variable.Lets Finish This Off.


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="jquery.js"></script>
    <script>
        function sayHello() {
            var txtName = $("#txtName").val();
            $.ajax({
                type: "POST",
                url: "Default.aspx/sayHello",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{'Name':'" + txtName + "'}",
                success: function (response) {
                    alert(response.d);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="text" id="txtName" />
            <br />
            <input type="button" id="btnSayHello" value="Say Hello" onclick="sayHello()" />
        </div>
    </form>
</body>

</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string sayHello(String Name)
    {
        return "Hello " + Name;
    }
}



No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...