Introduction:
Description:
In previous posts I explained How to Call Asp.net Page methods in jQuery, JavaScript restrict to enter only numbers, JavaScript Redirect to another page after some time delay, JavaScript Set Default home page in browsers and many articles relating to JavaScript, Asp.net. Now I will explain how to call server side methods or functions from JavaScript in asp.net.
In previous posts I explained How to Call Asp.net Page methods in jQuery, JavaScript restrict to enter only numbers, JavaScript Redirect to another page after some time delay, JavaScript Set Default home page in browsers and many articles relating to JavaScript, Asp.net. Now I will explain how to call server side methods or functions from JavaScript in asp.net.
By using asp.net
Ajax
scriptmanager we can call server side methods from JavaScript
for that we need to set property EnablePageMethods="True"
in asp.net Ajax
scriptmanager.
To implement this one you need to write the following code in your page
|
After
that add following namespace in code behind
C# Code
C# Code
using System;
using System.Web.Services;
|
Once namespaces added then write the following code
in code behind
[WebMethod]
public static string GetCurrentDate(string
name)
{
return "Hi "
+ name + Environment.NewLine + "Welcome to Aspdotnet-Suresh.com ";
}
|
VB.NET Code
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
hi suresh..have a doubt..
why have u made "GetCurrentDate" a [web method]??
cant it be a normal method? reply
bhatimohit@gmail.com
best example
yes i too have a same doubt that can't we call a normal method?
I dont want it as webmethod. Can it be done?
How to use external javascript file in asp.net.
hai suresh
why have u made "GetCurrentDate" a [web method]??
cant it be a normal method?
Why webmethod have to be static ? reply
mahasrijith@gmail.com
Note: Only a member of this blog may post a comment.