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, 1:29 AM EDT
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, 1:03 AM EDT
Very nice it's working for asp.net project
Andrew KelsonPosted Dec 13, 2016, 12:53 PM EST
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, 1:04 AM EDT
nice
sk guptaPosted Sep 19, 2015, 3:45 AM EDT
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, 1:04 AM EDT
fqef
abhi dasPosted Jan 28, 2015, 9:18 AM EST
thanks for this post.. you solved my lot of issue.
Manish Kumar ChoudharyPosted Nov 24, 2014, 1:25 AM EST
thanks sir.
ati MPosted Nov 24, 2014, 1:16 AM EST
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, 4:02 AM EDT
is this value storing in database too?
Robert JuradoPosted Aug 27, 2014, 9:26 PM EDT
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 22, 2014, 10:23 PM EDT
Thank you i got it.............................
Rizwan MohammadPosted May 22, 2014, 10:15 PM EDT
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, 9:10 AM EDT
you are right Bajinder.
bajinder MannPosted Oct 18, 2013, 5:25 AM EDT
Dear Sir, this code is not working on webuser controle please check and help me its very argent. tnks
Santosh YadavPosted Apr 20, 2013, 10:00 AM EDT
Great work sir...Very helpful article
Venkatesh EllurPosted Apr 18, 2013, 10:23 PM EDT
This is an excellent example.... Very Simple and Clear.. Thanks ABhimanyu.
sarfaraj mdPosted Dec 28, 2012, 12:57 AM EST
niceeee
Chirag SolankiPosted Aug 24, 2012, 5:56 AM EDT
thx . it help
shreeniwas kushwahPosted Aug 21, 2012, 4:40 AM EDT
Thanks Abhimanyu to this excellent example....!
Megha GoyaleditedPosted Jul 26, 2012, 6:55 PM EDTEdited Jul 26, 2012, 8:00 PM EDT
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, 6:30 AM EDT
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 VatsaTOP 100Posted Jul 23, 2012, 1:30 AM EDT
i have recorded the screen using camtasia.
Megha GoyalPosted Jul 23, 2012, 1:18 AM EDT
How to animate the pictures which you have post in this article please tell me the procedure of doing this.