Wednesday, February 12, 2020

How to use web method without using ajax in asp.net

Add Web Page 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Lab1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="scriptmanager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

            <button  onclick ="GetDate()">Click me</button>

        </div>
    </form>
    <script>
        function GetDate() {
            PageMethods.GetCurrentDate(onSuccess);
        }
        function onSuccess(response) {

            alert(" Current Date " + response);

        }
    </script>
</body>
</html>

Add the following code in WebForm2.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string GetCurrentDate()
        {
            return System.DateTime.Now.ToString();
        }






Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...