
March 17, 2025 07:55 by
Peter
in online forms. WebMethod and ScriptMethod Attributes are used by Net programs to call client-side scripts such as JavaScript and Jquery web calls. The shared function keyword from server-side pages is used in this technique. The function will send a post and receive a request on that, as indicated by the Shared keyword and WebMethod property that WebService exposes. The same kind of online technique will be utilized for sharing data in JSON format. Below is an example of the code.

Server End code
Public Class WebCall
Inherits System.Web.UI.Page
<WebMethod()>
<ScriptMethod()>
Public Shared Function GetServerTime() As String
Return DateTime.Now.ToString()
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' No server-side logic needed for this example.
End Sub
End Class
From the client end, an OnClick event will be sent a request for getting a server date & timely response. The client-side code example is below.
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"><main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
function callWebMethod() {
alert("callweb");
$.ajax({
type: "POST",
url: "WebCall.aspx/GetServerTime",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
$("#lblTime").text(msg.d);
},
error: function (xhr, status, error) {
alert("Error: " + xhr.responseText);
}
});
}
</script>
<div>
<button type="button" onclick="callWebMethod()">Get current server time</button><p id="btnGetTime"></p>
<label id="lblTime" Text=""></label>
</div>
</main>
</asp:Content>
Output
