Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Java Script - Call a Server Side Method in ASP.NET C-Sharp

Viewed:  2498 
Posted On:  11/02/2015 01:09:57 
How we can call a Server Side method using Java Script in ASP.NET C# ? 

In this article I am going to explain how we can call a Server Side method from Java Script in c#.

Below is my aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Java Script: Calling Server Side Method</title>

<script type="text/javascript" language="javascript">
        function jsCallServerSideMethod() {
            var _msg = $get("txtMessage").value;
            PageMethods.GetUserMessage(_msg, OnSuccess, OnFailure);

        function OnSuccess(result) {
            if (result) {
        function OnFailure(error) {



    <form id="form1" runat="server">


        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">

        <table style="border: solid 15px blue; width: 100%; vertical-align: central;" cellpadding="10"
                <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue;
                    font-family: 'Times New Roman'; font-size: 20pt; color: red;">
                    Java Script: Calling Server Side Method

                <td style="text-align: left; font-family: Times New Roman; font-size: 12pt;">

                    Enter Your Message #
                    <asp:TextBox ID="txtMessage" runat="server" Width="200px"></asp:TextBox>
                    <asp:Button ID="btnClick" runat="server" Text="Click" OnClientClick="jsCallServerSideMethod()" />






Now my aspx.cs code:

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 _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    public static string GetUserMessage(string message)
        return "Welcome " + message + Environment.NewLine + System.DateTime.Now;


Now run the application:


Image 1.


Image 2.


Image 3.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us