Another title of this article would be "Say bye-bye to Postbacks". Sometimes we need to stop annoying post-backs on ASP.NET Web Pages. For example, when one click the ASP.NET button on webpage, by default page gets post-back. So, how we stop this by keeping proper code-behind method calls. Let's look at one case.
I will create a simple form having two asp.Net textboxes and a button, we will enter our name and address in the textboxes and by clicking button will call the code-behind methods. For this we need to take the advantage of PageMethod and to setup PageMethod we need instance of ScriptManager on web page.
This is what we are going to create:

PageMethod is an easier and faster approach for ASP.NET AJAX. We can easily improve the user experience and the performance of web applications by unleashing the power of AJAX. One of the best things I like in AJAX is the PageMethod.
PageMethod is a way through which we can expose the server side page's methods in JavaScript. This brings so many opportunities; we can perform many operations without using slow and annoying post backs.
Let's create our HTML Page:
- <div>
- <p>Say bye-bey to Postbacks.</p>
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
- <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
- <br />
- <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
- <br />
- <asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="HandleIT(); return false;" />
- </div>
Note: Remember to add a new attribute EnablePageMethods and set it to true in the ScriptManager.
Now, look at the JavaScript Code:
- <head runat="server">
- <title></title>
- <script type="text/javascript">
- function HandleIT() {
- var name = document.getElementById('<%=txtname.ClientID %>').value;
- var address = document.getElementById('<%=txtaddress.ClientID %>').value;
- PageMethods.ProcessIT(name, address, onSucess, onError);
- function onSucess(result) {
- alert(result);
- }
- function onError(result) {
- alert('Something wrong.');
- }
- }
- </script>
- </head>
Now look at the code-behind of the C# (PageMethod):
- [WebMethod]
- ublic static string ProcessIT(string name, string address)
- {
- string result = "Welcome Mr. " + name + ". Your address is '" + address + "'.";
- return result;
- }
Remember to use the namespace "System.Web.Services" for WebMethods.
So, now we are all set to test it.

I hope you like it. Thanks.

Michel RodriguesPosted Jul 15, 2023, 5:29 AM
Hello! I am getting the following error message: WebForm5:17 Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received.and it is returning the aspx code in alert(), could you help me please?
sagar metkarPosted Oct 12, 2018, 5:03 AM
Very nice it's working for asp.net project
Andrew KelsonPosted Dec 13, 2016, 5:53 PM
How can I handle calling a second PageMethod upon a user's confirmation of the results from the first PageMethod? When I try this now, the alert in the second WebMethod's success function is never reached, even though the WebMethod executes as expected. Thanks.
Upendra Pratap ShahiPosted Sep 30, 2015, 5:04 AM
nice
sk guptaPosted Sep 19, 2015, 7:45 AM
Sir when i using "Calling WebService using Javascript" programme then an error occur which is as follows : "WebService Not Defined", so please suggest me a valuable help to resolve this. Thanks in Advance...
Deepan RajanPosted Jul 7, 2015, 5:04 AM
fqef
abhi dasPosted Jan 28, 2015, 2:18 PM
thanks for this post.. you solved my lot of issue.
Manish Kumar ChoudharyPosted Nov 24, 2014, 6:25 AM
thanks sir.
ati MPosted Nov 24, 2014, 6:16 AM
hi.thanks for your post. i want use result value to change a label text. some thing such as : $("#LblOrderVal").attr("text", result); in onSucess function . but it doesnt work
saurabh saxenaPosted Sep 11, 2014, 8:02 AM
is this value storing in database too?
Robert JuradoPosted Aug 28, 2014, 1:26 AM
If I want to throw an error and/or return a string value from the PageMethod, how do I do that? Thanks in advance.
Rizwan MohammadPosted May 23, 2014, 2:23 AM
Thank you i got it.............................
Rizwan MohammadPosted May 23, 2014, 2:15 AM
Hi friends hear code work but i got an error call Uncaught ReferenceError: PageMethods is not defined so plz tell me how can i resolve this is error
Santosh YadavPosted Oct 18, 2013, 1:10 PM
you are right Bajinder.
bajinder MannPosted Oct 18, 2013, 9:25 AM
Dear Sir, this code is not working on webuser controle please check and help me its very argent. tnks
Santosh YadavPosted Apr 20, 2013, 2:00 PM
Great work sir...Very helpful article
Venkatesh EllurPosted Apr 19, 2013, 2:23 AM
This is an excellent example.... Very Simple and Clear.. Thanks ABhimanyu.
sarfaraj mdPosted Dec 28, 2012, 5:57 AM
niceeee
Chirag SolankiPosted Aug 24, 2012, 9:56 AM
thx . it help
shreeniwas kushwahPosted Aug 21, 2012, 8:40 AM
Thanks Abhimanyu to this excellent example....!
Megha GoyaleditedPosted Jul 26, 2012, 10:55 PMEdited Jul 27, 2012, 12:00 AM
Thanks Abhimanyu i used camtasia but can u tell me how can i convert my video in camtasia as an image.
Akil A MPosted Jul 25, 2012, 10:30 AM
Okey,,, if we want to get the value from Database then. how it work.. please Explain me with one example please. my email id is [email protected]
Abhimanyu K VatsaPosted Jul 23, 2012, 5:30 AM
i have recorded the screen using camtasia.
Megha GoyalPosted Jul 23, 2012, 5:18 AM
How to animate the pictures which you have post in this article please tell me the procedure of doing this.