Calling c# function through AJAX in ASP.NET (Web method function)

Calling c# function through AJAX in ASP.NET

  This article will help you to call the ASP.net function through AJAX. we are using below form for example.
Calling c# function through AJAX in ASP.NET
Calling C# function through ASP.NET

Here we are using EnablePageMethods in the script manager. so we can able to call exposed methods in the back end.
<form id="form1" runat="server">
    <div>
        <asp:ScriptManager EnablePageMethods="true" ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <fieldset id="ContactingFieldSet">
        <label>Name: <input type="text" id="txtName" /></label><br />
        <label>Contact Number: <input type="text" id="txtContactNumber" /></label><br />
        <label>Email Id: <input type="text" id="txtEmailId" /></label><br />
        <button onclick="SendingForm();">Submit Data</button><br />
        </fieldset>
    </div>
    </form>
 

from the above button we are calling the SEndingForm() button from the below javascript code.

    <script type="text/javascript">
        function SendingForm() {
            debugger;
            var custName = $get("txtName").value;
            var custContactNumber = $get("txtContactNumber").value;
            var custEmailId = $get("txtEmailId").value;
            //alert(custEmailId);
            PageMethods.SendingForm(custName, custContactNumber, custEmailId,OnSucceeded, OnFailed);
        }
        function OnSucceeded() {
            // Dispaly "thank you."
            $get("ContactFieldset").innerHTML = "<p>Thank you!</p>";
        }

        function OnFailed(error) {
            // Alert user to the error.
            alert(error.get_message());
        }
    </script>

 AJAX will automatically call the SendingForm method which is exposed through web method.

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

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


    [WebMethod]
    public static void SendingForm(string custName, string contactNumber, string emailId)
    {
        if (string.IsNullOrEmpty(custName))
        {
            throw new Exception("Please enter customerNmae");
        }
        if (string.IsNullOrEmpty(contactNumber))
        {
            throw new Exception("Please enter contact number");
        }
        if (string.IsNullOrEmpty(emailId))
        {
            throw new Exception("Please enter email id");
        }
        //return "success";
    }
}

If you have any queries or suggestions, please fell free to ask in comments section.
Share this post :

+ comments + 2 comments

25 April 2012 at 20:32

clean example. keep your good job :)

25 April 2012 at 21:22

Thanks for complements... :) This is first comment in my blog. thanks once again.

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar