Skip to main content

Getting Started with ASP.NET AJAX

I have been doing AJAX in ASP.NET for a while using frameworks like Anthem.Net and AJAX.net. I did not use Atlas as Atlas was still in development and more over our client has not upgraded to .Net 2.0 yet.

I have started playing with Microsoft ASP.NET AJAX (formerly Atlas) for a week now. At first I was really overwhelmed with the whole Atlas documentation but felt a little comfortable after watching the Ajax videos on ASP.NET website .

Here I will go through the process of getting started with Microsoft ASP.NET AJAX. You can access this post here on Google docs.
http://docs.google.com/View?docid=dd8p9vd3_7dzckx7

Install MS AJAX
Download and install Atlas from here.(http://ajax.asp.net/downloads/default.aspx?tabid=47)

Visual Studio Tool Box
After installing MS AJAX, you should see AJAX Extensions tab in the visual studio tool box.

New ASP.NET project template
A new project template that allows you to create ASP.NET AJAX enabled Web applications is also installed on installing Atlas.

Programming with Atlas
Atlas offers you two ways of building AJAX functionality into your website.
1) Using Update Panel Control to do partial rendering
2) Ability to place calls to remote endpoints.

Update Panel
Using Update Panel greatly simplifies AJAX programming. All you have to do is wrap the controls that need to be updated partially and asynchronously inside the update panel. The Update panel intercepts any form submissions and data being sent and routes them through XMLHttpRequest to the server. When the response is ready, the update panel updates the DOM tree by updating only the controls which are part of the update panel. The beauty of the update panel lies in the fact that it takes care of placing AJAX calls, updating the ViewState and doing partial rendering with out us having to write a single piece of javascript.

To drive home the point, let us create a simple ASP.NET project.
The project will have have one webform (Default.aspx) with a GridView control in it.
The GridView will display a list of customers from a database (ShoppinCart).

Create the Database
Please create a database with the name ShopoingCart and a table called Customer in that database. The customer table will have the following columns. Add those columns and populate the table with some data. (CustomerId, FirstName, LastName, Phone, Email)

Create the Project
Open Visual Studio 2005 and create a new project of type ASP.NET AJAX-Enabled Web Application. Name the project AJAXTest.

Add the connection string to Web.config (Modify the connection string accordingly)


Add UpdatePanel to the webform
On to your Default.aspx page, drag and drop an UpdatePanel control from the tool box (under AJAX Extension tab)

Drag and drop GridView
Drag and drop a gridview into the updatepanel. Also drag and drop SqlDataSource control inside the update panel. If you created the database and customer table as directed above, your can replace your update panel with the code below.



Test the Page
Set default.aspx as your start page and run the project. As you click edit, updata and delete controls in the grid, you should
notice that your page does not do a full refresh, only the grid is updated.

Conclusion
We have seen how easy it is to add AJAX functionality to your webform with update panel contro. In the next article I will demonstrate
how to call web services with Atlas.

Comments

Popular posts from this blog

Clear Validation Errors and Validation Summary messages

ASP.net built in validation does not provide us a straight forward to clear all the validation errors. This would be really helpful while resetting a form. The reset html button would simply reset the form values but will not clear the validation errors. The following javascript code snippet can be used to clear the validation error messages. Have a reset button on your form and call the following js function onclick. <input type="reset" onclick="HideValidationErrors();" /> function HideValidationErrors() { //Hide all validation errors if (window.Page_Validators) for (var vI = 0; vI < Page_Validators.length; vI++) { var vValidator = Page_Validators[vI]; vValidator.isvalid = true; ValidatorUpdateDisplay(vValidator); } //Hide all validaiton summaries if (typeof (Page_ValidationSummaries) != "undefined") { //hide the validation summaries

Kill a remote user session remotely

When trying to connect to your Windows 2000/2003 server remotely, you may receive the following error. "The terminal server has exceeded the maximum number of allowed connections." You could kill one or more of those connections by using PsExec tool that can be downloaded from the following link. This tool and a bunch of others were developed by SysInternals which was bought by Microsoft. http://www.microsoft.com/technet/sysinternals/utilities/pstools.mspx Open your command prompt and from the directory that contains the psexec utility, do the following 1) psexec \\x.x.x.x -u user -p password cmd (this will give you access to the cmd prompt on the server) Example: psexec \\127.0.0.1 -u admin -p password cmd 2) once you get the command prompt run the command qwinsta to get a list of all Terminal Services connections. Each connection has an Id Number. 3) Run the command logoff [id# of session to quit] /v (this will kill the connection with that id #) Example: logoff 2 /v Once

Creating a Windows Task Scheduler Service

Recently, one of my friends asked me for help in creating a windows service for scheduling some tasks. He said that he could not use the task scheduler that comes with Windows as the Task Scheduler expects each task to be a stand alone executable file. All his tasks were in a single class library and it would be a lot of work to separate each task into its own executable. Also he said that that the schedule for the tasks could change from time to time and it would be nice if he could configure the tasks in an XML file. To Recap, the requirements for the windows service are 1) The tasks should be loaded from a class library 2) The schedule information for the tasks should be configurable in an XML file. We googled (binged :)) for the solution and found an excellent article by Ajit Kumar Application Scheduler Service Using C#.Net And XML to base our solution upon. We took some good points like configuring the task information from the above mentioned article. This is how we to