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

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

Article:
Viewed:  1271 
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 asp.net 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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) {
                alert(result);
            }
       }
        function OnFailure(error) {

        }

    </script> 

</head>
<body>
    <form id="form1" runat="server">

    <div>

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

        <table style="border: solid 15px blue; width: 100%; vertical-align: central;" cellpadding="10"
            cellspacing="10">
            <tr>
                <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>
            </tr>
            <tr>

                <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()" />

                </td>

            </tr>
        </table>

    </div>
    </form>
</body>

</html>

 

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)
    { 

    } 

    [WebMethod]
    public static string GetUserMessage(string message)
    {
        return "Welcome " + message + Environment.NewLine + System.DateTime.Now;
    }
}

 

Now run the application:

1.png

Image 1.

2.png

Image 2.

3.png

Image 3.

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