Tutorial: Introduction to AJAX in ASP.NET 2.0 and VB


V4 Ajax Tutorials
Server Intellect Cloud Hosting

Tutorial: Introduction to AJAX in ASP.NET 2.0 and VB

AJAX, short for Asynchronous JavaScript And XML, isn't a technology but rather a grouping of technologies. AJAX uses a communication technology (typically SOAP and XML) to send and receive an asynchronous request/response to the server, and then leverages presentation technologies (JavaScript, DOM, HTML, and CSS) to process the response. Applications using AJAX are legitimate today, because most browsers support the necessary technology.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

First thing we need to do is to import the AJAX namespace from Ajax.DLL.The Ajax namespace contains Ajax.dll encapsule Asynchronous Javascript and XML in Bin folder. This is an combination of Javascript and XML. The data is transfered in the form of XML.

Imports Ajax
Imports System.Data.SqlClient
Imports System.Data

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

In this tutorial, the only configuration step beyond that is to add the following code in the web.config file, inside the <system.web> element.

<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>

In order to make server-side functions available through JavaScript, two things must be done. First, the function or functions in question must be marked with the Ajax.AjaxMethodAttribute. Second, the class containing these functions must be registered through a call to Ajax.Utility.RegisterTypeForAjax during the page load event. Then we use GetData() even of btnGetData and display() event of DropDownList  to do the work.  The button btnGetData is a html button. It display employee data without page post-back. When chose a particular record in the Dropdownlist, the employee name and employee ID will be displayed in the label.

The code below can go in the <head> tags of the ASPX page:

<script language="javascript" type="text/javascript">
function GetData()
{
var response;
Ajax_CSharp.GetData(GetData_CallBack);
}

function GetData_CallBack(response)
{
var response=response.value;

if(response=="Empty")
{
alert("No Record Found !!!");
}
else if(response=='Error')
{
alert("An Error occured in accessing the DataBase !!!");
}
else
{
var arr=response.split("~");
var empID=arr[0].split(",");
var empName=arr[1].split(",");

document.getElementById('dlistEmployee').length=0;
for(var i=0;i<empID.length;i++)
{
var o = document.createElement("option");
o.value = empID[i];
o.text = empName[i];
document.getElementById('dlistEmployee').add(o);
}
}
}

function display()
{
var selIndex=document.getElementById("dlistEmployee").selectedIndex;
var empName=document.getElementById("dlistEmployee").options(selIndex).text;
var empID=document.getElementById("dlistEmployee").options(selIndex).value;

document.getElementById("lblResult").innerHTML="You have selected " + empName + " ( ID : " + empID + " )";
}
</script>

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.

The front end AjaxDataSearchVB.aspx page looks something like this:

<div align="center">
<input id="btnGetData" onclick="GetData();" type="button" value="To Get Employee Data From DB" style="width: 203px" />
&nbsp;&nbsp;
<asp:DropDownList ID="dlistEmployee" runat="server" onchange="display();">
</asp:DropDownList><asp:Label ID="lblResult" runat="server" Text="No Record Selected"></asp:Label>
</div>

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.

Register Ajax in PageLoad and get data from the employee table of  Database.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Ajax.Utility.RegisterTypeForAjax(GetType(Ajax_CSharp))
End Sub

<Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)> _
Public Function GetData() As String
Try
Dim con As New SqlConnection(ConfigurationManager.AppSettings("conn"))
Dim cmd As New SqlCommand("SELECT * FROM employee", con)
cmd.Connection.Open()

Dim adapter As New SqlDataAdapter(cmd)
Dim ds As New DataSet("DataSet")
adapter.Fill(ds, "Table")

If ds.Tables(0).Rows.Count <= 0 Then
Return "Empty"
Else
Dim empID As String = ""
Dim empName As String = ""
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
empID += ds.Tables(0).Rows(i)(0).ToString() + ","
empName += ds.Tables(0).Rows(i)(1).ToString() + ","
Next i
empID = empID.Substring(0, empID.Length - 1)
empName = empName.Substring(0, empName.Length - 1)

Return empID + "~" + empName
End If
Catch
Return "Error"
End Try
End Function

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The flow for the code behind page is as follows.

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Ajax

Class Ajax_CSharp
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Ajax.Utility.RegisterTypeForAjax(GetType(Ajax_CSharp))
End Sub

<Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)> _
Public Function GetData() As String
Try
Dim con As New SqlConnection(ConfigurationManager.AppSettings("conn"))
Dim cmd As New SqlCommand("SELECT * FROM employee", con)
cmd.Connection.Open()

Dim adapter As New SqlDataAdapter(cmd)
Dim ds As New DataSet("DataSet")
adapter.Fill(ds, "Table")

If ds.Tables(0).Rows.Count <= 0 Then
Return "Empty"
Else
Dim empID As String = ""
Dim empName As String = ""
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
empID += ds.Tables(0).Rows(i)(0).ToString() + ","
empName += ds.Tables(0).Rows(i)(1).ToString() + ","
Next i
empID = empID.Substring(0, empID.Length - 1)
empName = empName.Substring(0, empName.Length - 1)

Return empID + "~" + empName
End If
Catch
Return "Error"
End Try
End Function
End Class

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
Kumar said:

Great intro for beginners. thanks

http://www.planetsourcecode.in

Posted May 11, 2009 11:16 AM
Infant Snowsuit said:

Never knew what AJAX actually stood for, before reading this article. On first glance, it looks like it could be a fairly useful tool.

Posted Nov 2, 2010 5:06 PM
$1 viagra said:

Thanks for taking time to write this post. good review.

Posted Jan 31, 2011 2:28 PM
waikiki car rental said:

The coding examples are very useful. I like your post a lot.

Posted Feb 10, 2011 5:17 AM
PDF Tools Review said:

Read The Best PDF Tools Review

Posted May 14, 2011 7:16 AM
fashion said:

The key line is where the document is added to the current user's queue, which adds it to the session. Next, we'll create a user control, which can be placed on any page, used to notify the user when a queued document has become available. This user control will contain a single AJAX method, along with the code necessary to register the class with AJAX.

Posted May 21, 2011 4:35 PM
tissot couturier watch said:

Its really fascinating, but you dont signify it well at all, man. Anyway, in my language, there usually are not much good supply like this.

Posted Jul 5, 2011 2:56 AM
wholesale pandora charms said:

I imply, I dont need to sound like a know-it-all or anything, however could you could have presumably put a little bit extra effort into this subject.

Posted Jul 5, 2011 2:56 AM
Louis Vuitton said:

Drive to Stem Shingles Meets Few Expectations

Posted Jul 12, 2011 10:39 PM
cartier bracelet love said:

I found your post really helpful. Thanks for posting such informative content. Keep posting.

http://www.ctlove2u.org

Posted Jul 13, 2011 6:19 AM
cartier bracelet love said:

I found your post really helpful. Thanks for posting such informative content. Keep posting.

http://www.ctlove2u.org

Posted Jul 13, 2011 6:20 AM
Guess online store said:

I had already downloadd it, so thanks for the show! http://www.buycheapguess.org

Posted Jul 16, 2011 1:08 AM
discounttiffany said:

<A href=" http://www.discounttiffanyweb.com"> Wholesale Tiffany Jewelry </A>

<A href=" http:// www.discounttiffanyweb.com "> Cheap tiffany jewelry </A>

<A href=" http:// www.discounttiffanyweb.com "> Discount tiffany jewelry </A>

<A href=" http:// www.discounttiffanyweb.com "> Tiffany Silver </A>

<A href=" http:// www.discounttiffanyweb.com "> Tiffany rings </A>

<A href=" http:// www.discounttiffanyweb.com "> Tiffany Bracelets </A>

<A href=" http:// www.discounttiffanyweb.com "> Tiffany Necklace </A>

<A href=" http:// www.discounttiffanyweb.com "> Tiffany Sets </A>

<A href=" http:// www.discounttiffanyweb.com "> Tiffany Earrings </A>

<A href=" http:// www.discounttiffanyweb.com "> tiffany outlet </A>

Posted Jul 30, 2011 9:19 AM
Hermes said:

with blue accents

Posted Jul 31, 2011 9:03 PM
aglla said:

nice topic

<a href="http://aglla.com">شبكة اغلى</a>

<a href="http://www.nwahy.info">موقع نواحي - دليلك الشامل</a>

Posted Aug 6, 2011 1:50 PM
トリーバーチ said:

トリーバーチ(Tory Burch)正規品の激安販売店舗,トリーバーチ 財布,靴 最高60パーセントオフ

Posted Aug 14, 2011 1:37 PM
トリーバーチ said:

トリーバーチ(Tory Burch)正規品の激安販売店舗,トリーバーチ 財布,靴 最高60パーセントオフ

Posted Aug 14, 2011 1:37 PM
ugg boots said:
Posted Aug 16, 2011 10:40 PM
tiffany said:

<a href="http://www.discounttiffanyweb.com">wholesale tiffany jewelry </a>

<a href="http://www.discounttiffanyweb.com">discount tiffany jewelry </a>

<a href="http://www.discounttiffanyweb.com">cheap tiffany jewelry </a>

<a href="http://www.discounttiffanyweb.com"> tiffany jewelry </a>

<a href="http://www.discounttiffanyweb.com">tiffany silver </a>

<a href="http://www.discounttiffanyweb.com">tiffany rings </a>

<a href="http://www.discounttiffanyweb.com">tiffany nacklaces </a>

Posted Aug 21, 2011 9:33 AM
Mobile Tracking said:

The article contains highly appreciable content, that is highly appreciable good to read this wonderful content. Keep it up. <a href="http://www.mobiletrackingsoftware.co/">iPhone Tracking App</a>

Posted Sep 6, 2011 4:09 AM
dividend paying stocks said:

Thanks for going through the code step by step. made it easy to follow and set up. i also like that you can email it if i want the whole thing.

Posted Sep 13, 2011 4:47 PM
anna said:

thanks

Posted Sep 22, 2011 3:46 AM
anna said:

www.frankandmarshallclothing.co.uk

Posted Sep 22, 2011 3:46 AM
Huse i Thailand said:

It was very well authored and easy to understand. Unlike additional blogs I have read which are really not that good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!

Posted Oct 3, 2011 12:50 PM
Air Jordan Original Shoes said:

Its really fascinating, but you dont signify it well at all, man.

Posted Oct 8, 2011 4:12 AM
shoppingpandora said:

It’s really a nice and helpful piece of information.Long time no see so excellent article, and I am very interested in your article, but also very much hope you can come to visit our website<a href="http://www.shoppingpandora.net" >shoppingpandora</a>&<a href="http://www.shoppingpandora.net/pandora-charm-collection.html" >Pandora Charm Collection</a>

Posted Oct 9, 2011 9:58 PM
Air Jordan said:

dxvdf

Posted Oct 22, 2011 3:17 AM
Air Jordan said:

It’s really a nice and helpful piece of information.Lon

Posted Oct 22, 2011 3:17 AM
Air Jordan said:

It’s really a nice and helpful piece of information.Long time no see so excellent article, and I am very interested in your article, but also very much hope you can come to visit our website

Posted Oct 22, 2011 3:17 AM
Antakya Biberi said:

antakya biberi zayiflama hapi antakya biber hapi

Posted Oct 23, 2011 7:43 AM
Antakya Biberi said:

antakya biberi zayiflama hapi antakya biber hapi

Posted Oct 23, 2011 7:43 AM
zayıflama said:

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

Posted Oct 23, 2011 7:49 AM
Nike Heels For Women said:

Most ladies like high heels shoes Nike Heels For Women This new Nike Heels is very beautiful, and the Nike Dunk Heels for women have been popularized trendy. The popularity of Nike High Heels has been greatly aided by its easy customization.

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.

Ken Griffey Shoes may be best because of how durable they are.

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:19 AM
Y3 trainers said:

Most ladies like high heels shoes Nike Heels For Women This new Nike Heels is very beautiful, and the Nike Dunk Heels for women have been popularized trendy. The popularity of Nike High Heels has been greatly aided by its easy customization.

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.

Ken Griffey Shoes may be best because of how durable they are.

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:21 AM
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.http://www.y3-trainers.com

Posted Oct 26, 2011 4:22 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. http://www.kengriffeys2011.com

Posted Oct 26, 2011 4:23 AM
coseur said:

dotjs,mnrlsekurer

Posted Nov 1, 2011 9:13 PM
Daniee said:

dresshttp://www.theydress.com/brautkleider.html

Posted Nov 2, 2011 8:13 PM
Maurers hapı said:

Maurers ile termojenilk olarak yağlarınızdan kurtulabilirsiniz.

Posted Nov 25, 2011 7:40 PM
African mango hapı said:

Dr. Mehmet özün tavsiyesi olan african mango hapını kullanabilirsiniz.

Posted Nov 25, 2011 7:42 PM
Antakya biberi said:

Mesut yarın 40 kilo verdiren mucizevi bitkilerden karışım olan antakya biberi sizlerle yavrular

Posted Nov 25, 2011 7:43 PM
Ken Griffey Jr Shoes said:

This is my first time i visit here. I found so many helpful stuff in your website especially its discussion. From the tons of reviews on your articles or blog posts, I guess I am not the only one having all the enjoyment here! keep up the excellent work. My kind regards.

Posted Dec 15, 2011 10:55 PM
Flagstang said:

Your post is really good and informative. I'm surprised that your post has not gotten any good quality, genuine comments. You have done a great job by posting this article.Thanks

Posted Dec 16, 2011 7:22 AM
Cheap nike free said:

wearing a "yellow robe" a stunning appearance in Shanghai endorsement deals, it seems glamorous but also blowing

Posted Dec 24, 2011 9:12 AM
Azithromycin uses said:

I found so many helpful stuff in your website especially its discussion. From the tons of reviews on your articles or blog posts, I guess I am not the only one having all the enjoyment here! keep up the excellent work.

Posted Dec 29, 2011 11:19 PM
MCX Tips said:

Thanks for a nice share you have given to us with such an large collection of information. Great work you have done by sharing them to all. simply superb

Posted Jan 5, 2012 5:50 AM
Ken Griffey Shoes said:

Thanks for the write up! This is really some great stuff here! Wishing for a following post for the similar subject.

Posted Jan 5, 2012 10:29 PM
Ken Griffey Shoes said:

I really like this information!Thanks! Your article is wonderful,can you tell me how did you do it?Your blog is wonderful,I like it very much.

Posted Jan 16, 2012 5:13 AM
Ken Griffey Shoes said:

From the tons of reviews on your articles or blog posts, I guess I am not the only one having all the enjoyment here! keep up the excellent work.

Posted Jan 18, 2012 1:21 AM
Air Jordan said:

I guess I am not the only one having all the enjoyment here! keep up the excellent work.

Posted Feb 3, 2012 11:53 PM

Leave a Comment