AJAX Tutorial Page Methods in ASP.NET


V4 Ajax Tutorials
Server Intellect Cloud Hosting

AJAX Tutorial Page Methods in ASP.NET

 

Getting Started
If you are looking to learn about Page Methods in ASP.NET or Wanted to Learn AJAX or looking for an AJAX Tutorial then you have reached your destination.
When starting out with AJAX in ASP.NET, it can be difficult to determine a good starting point. Visual Web Developer has loads of AJAX controls available to us to help get the job done, but some may be overkill for simple AJAX page functionality. A great place to start is with Page Methods, which allow you to call ASP.NET functions from your page via the wonderful and oh so amazing Javascript. This is a very powerful feature since all of the server communication is done behind the scenes, preventing a refresh of the page your visitor is currently on.
What are we doing?
We’ll build a simple “contact us” form that will allow a visitor to enter their name, their email address, and a comment. Normally, after the user enters the information, we would use some sort of validation technique to verify that the data entered was “good” data, and then redirect the user to some sort of confirmation page letting them know that their message has been sent. Our implementation will be slightly different in that there will be no redirect, and we’ll be using an ASP.NET page method to call an ASP.NET function from our code behind via Javascript.
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
What you’ll need
Some knowledge of HTML
Some knowledge of Javascript
Some knowledge of ASP.NET
A copy of Visual Web Developer or Visual Studio installed on your PC
A web browser
 
Let’s build something
We’ll want to start a new project by going into the file menu selecting new project,and then selecting Empty ASP.NET web application. Name the project Contact. This’ll set us up with a basic web application file structure, and allow us to start building our project. Next, add an ASP.NET web form to the project by right-clicking on the application in the solution explorer, selecting Add New Item and then selecting Web Form. Name the form ContactUs.aspx, and click Add.
 
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
 
The code for the form is very simple, and only consists of a fieldset, 3 labels, 2 input fields for a name and email, a textarea for the comment, and a button to submit. This is how your ContactUs.aspx form should look:
<form id="form1" runat="server">
<fieldset id="contactFieldSet">
<label>
Your Name   <br />
<input type='text' id='txtName' /></label>
<label>
Email   <br />
<input type='text' id='txtEmail' /></label>
<label>
Comment <br />
<textarea id="txtComment"></textarea></label>
<button id="send" onclick="SendForm(); return false;"> Send </button>
</fieldset>
</form>
 
Now we’ll add a Script Manager control to our page. This control manages our client side scripting, making it possible to perform AJAX calls on our page. Adding one is simple, and only requires that you drag a Script Manager control from the toolbox, and place it somewhere on the page. Once you’ve done that, go into the properties for the script manager control and set the property called EnablePageMethods to true. Notice that the button on our page has the onclick event handler set to call the function SendForm(). This function is a Javascript function, however since we just enabled page methods, we can also call ASP.NET functions from within it. Here’s the code behind file for our ContactUs.aspx.cs:
using System.Web.UI.WebControls;
using System.Web.Services;
 
namespace Contact
{
public partial class ContactUs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){
 
}
 
[WebMethod]
public static void SendForm(string name,string email,string message){
 
}
}
}
 
The first thing to notice is that I’ve added the System.Web.Services namespace. This is so that we can use the [WebMethod] attribute in our code behind. This attribute basically allows Javascript to call a method from the client side, whereas normally it would have to be called by ASP on the server side. Let’s take a look at the Javascript file and all its glory. You’ll see that I’m going to call “PageMethods.SendForm”. That code is automatically built by ASP.NET because we added the [WebMethod] attribute to our SendForm function in the code behind file.
function SendForm() {
var name, email, message, error;
 
name = $get('txtName').value;
email = $get('txtEmail').value;
message = $get('txtComment').value;
 
PageMethods.SendForm(name, email, message, OnSucceeded);
};
function OnSucceeded() {
$get('contactFieldSet').innerHTML = '<p> Thank you! Your comment was sent</p>';
};
 
Now, when the user enters data into our form and clicks send, they’ll see a warm and happy “Thank you!” without having to be redirected to another page, or without the page refreshing.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Final Thoughts
This has been a simple example of how to implement Page Methods in ASP.NET. We can now call functions from our code behind files using Javascript, and all of the normal request/response processing is done behind the scenes away from the eyes of our visitors. ASP.NET AJAX is really simple to use, and hopefully this will inspire you to build bigger, better AJAX applications. Enjoy!
 


Comments
mahasweta mohanty said:

Hi,

Where to write the below code.

function SendForm() {

var name, email, message, error;

name = $get('txtName').value;

email = $get('txtEmail').value;

message = $get('txtComment').value;

PageMethods.SendForm(name, email, message, OnSucceeded);

};

function OnSucceeded() {

$get('contactFieldSet').innerHTML = '<p> Thank you! Your co

Please reply me ASAP.

Regards,

Mahasweta

Posted Sep 20, 2010 6:49 AM
Trin said:

You can add it to the separate js file or you can add script to the same page. For example::

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title></title>

<script>

function SendForm() {

var name, email, message, error;

name = $get('txtName').value;

email = $get('txtEmail').value;

message = $get('txtComment').value;

PageMethods.SendForm(name, email, message, OnSucceeded);

};

function OnSucceeded() {

$get('contactFieldSet').innerHTML = '<p> Thank you! Your comment was sent</p>';

};

</script>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

<fieldset id="contactFieldSet">

<label>

Your Name <br />

<input type='text' id='txtName' />

</label>

<label>

Email <br />

<input type='text' id='txtEmail' />

</label>

<label>

Comment <br />

<textarea id="txtComment"></textarea>

</label>

<button id="send" onclick="SendForm(); return false;"> Send </button>

</fieldset>

</form>

</body>

</html>

Posted Sep 29, 2010 6:59 AM
Freelance SEO India said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Dec 29, 2010 10:17 PM
cialis mastercard said:

The excellent tutorial. I admire with your knowledge.

Posted Mar 5, 2011 9:45 AM
cheap meds store said:

It was nice to read your blog. AJAX excellent

Posted Jun 24, 2011 5:32 AM
zyril said:

nice

Posted Jul 7, 2011 1:04 AM
Dover water treatment said:

Wonderful info about the web program,Im pleased I found it though, ill be checking back soon...

Posted Jul 9, 2011 2:29 AM
Custom Dissertation Writing said:

This diary shows that you genuinely cognize to activity the alter of your arena, dry and grouping from all over the mankind. Thanks a lot.

Posted Jul 11, 2011 6:31 AM
Louis Vuitton said:

Drive to Stem Shingles Meets Few Expectations

Posted Jul 12, 2011 10:42 PM
Schoolwear clothing said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Jul 18, 2011 2:22 AM
Hermes said:

The i cars also signal

Posted Jul 31, 2011 8:43 PM
seo company jersey said:

We will surely keep updating and following your thread for more information. It was a nice idea.  Keep it  up.

Posted Aug 2, 2011 2:58 AM
www.prada-miumiu.com said:

www.prada-miumiu.com

Posted Aug 6, 2011 3:22 AM
Hua Hin i Thailand said:

There are plenty of information about this topic in the net & some are definitely better than others.

Posted Sep 7, 2011 10:52 AM
Must University said:

I agree with your opinion and fully support it, you have been a great contributor and I always come to your blog as I know you always share the best of your information.

Posted Sep 30, 2011 4:15 AM
Belford University said:

Thanks for sharing this information with us. I am a big fan of reading, thanks for sharing this wonderful information.

Posted Sep 30, 2011 4:18 AM
pplplp said:

ijijiji

Posted Oct 21, 2011 2:58 PM
Asics Running said:

http://www.asicsaustore.com/ Asics Running

Asics Shoes

Asics Tiger

Onitsuka Tiger Sale

Posted Oct 21, 2011 9:28 PM
Antakya Biberi said:

antakya biberi zayiflama hapi antakya biber hapi

Posted Oct 23, 2011 7:38 AM
maurers said:

maurers zayıflama hapı termojenik zayıflama hapları zayıflama ile zayıflama bölgesel zayıflama

Posted Oct 23, 2011 7:46 AM
Ken Griffey Shoes said:

Manufactured with some of the finest materials in the industry,Ken Griffey Jr Shoes you will be pleased that your purchase Ken Griffeys will not only last, but that your body will have the ergonomic support it deserves.

Posted Oct 26, 2011 4:29 AM
Adidas Y3 Trainers said:

Adidas Y3 Trainers became a part of the Adidas group, and today they have become a recognised brand partnership around the world. But at the time Y3 trainers were largely being developed as a men's fashion item. Y3 Trainers UK offering both style and performance.

Posted Oct 26, 2011 4:30 AM
Adidas Jeremy Scott said:

My good friends recommend this site to me.I have to say that the content

here was the most completed that I found anywhere.I will be back to

check some more information on this.Thanks

Posted Nov 19, 2011 3:50 AM
personal statement essay said:

I just came across your blog and reading your beautiful words. I thought I would leave my first comment but I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Posted Dec 13, 2011 1:56 AM
web design Hertfordshire said:

Brief description given keep it up.

Posted Dec 14, 2011 2:51 AM
achat cialis sans ordonnance said:

Wonderful post! You`ve made some very astute observations and I am thankful for the the effort you have put into your writing. Its clear that you know what you are talking about. I am looking forward to reading more of your sites content.

Posted Dec 20, 2011 3:21 AM
Flagstænger said:

I am happy when reading your blog with updated information! thanks alot and hope that you will post more site that are related to this site.

Posted Dec 23, 2011 3:19 PM
led displays said:

Great stuff of your stuff, man. I’ve read your stuff before and you’re too awesome. I enjoy what you’ve got here, love what you’re saying and exactly how you say it. You create it entertaining but you just seem to ensure that is stays smart. I can’t wait to see more of your stuff. This is really a fantastic blog.

Posted Dec 24, 2011 2:47 PM
Nike heels said:

, it can be difficult to determine a good starting point. Visual Web Developer has loads of AJAX controls available to us to help get the job done, but some may be overkill for simple AJAX page

Posted Jan 14, 2012 3:09 AM
Mallorca property said:

I am a newbie in this domain. I am sure that this domain will help me to learn Ajax

Posted Jan 17, 2012 9:40 AM
Law school personal statement writing service said:

This is really awe some!

Posted Feb 1, 2012 4:30 AM
Gold Name Necklaces said:

ASP.NET AJAX is really simple to use, and hopefully this will inspire you to build bigger, better AJAX applications. Enjoy!

Posted Feb 2, 2012 7:32 AM
Gold Name Necklaces said:

ASP.NET AJAX is really simple to use, and hopefully this will inspire you to build bigger, better AJAX applications. Enjoy!

Posted Feb 2, 2012 7:32 AM

Leave a Comment