All in one forum  - Applications | Games | E-Books | Music, Movies & Videos | Mobile Stuff | Live Discussions | Webmaster Stuff | Many More | Community to Hang Out and Stick to
Search Today's Posts Mark Forums Read

Go Back   Home > Tutorial Section > Programming > ASP
Reload this Page [Tutorial] Easiest way to implement Ajax in your ASP.Net 2.0 applications
Forgot Password? Join Us!
ASP Post your Active Server Pages Tutorial Here

Notices
Your link here Your link here Your link here Your link here Your link here

Your Ad Here


Rate This Thread - Easiest way to implement Ajax in your ASP.Net 2.0 applications.

Post New Thread Reply
Bookmarks
 
LinkBack Thread Tools Display Modes
Old 05-23-2008, 06:13 AM   #1 (permalink)
 
hacks's Avatar
 
User Info
Join Date: Oct 2007
Achievements Posts: 1,645
Casino Cash: $189980

Total Points: 494,020.37
Donate

Reputation: 255683
hacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond reputehacks has a reputation beyond repute


Awards Showcase
Member of the Month 
Total Awards: 1
Easiest way to implement Ajax in your ASP.Net 2.0 applications

In this article i am giving you a very usefull example, that is when you choose the country in a dropdown list which is filled dynamically and want the state comes in another dropdown will depend on the first dropdown list. To remove this post back i use ajax here.

Introduction

Asynchronous JavaScript and xml (AJAX) is now the need of the new generation websites. In the asp.net using AJAX is very easy. We just place few line codes in the web.config file and start using Ajax tags. But you should download Ajaxtoolkit first from the following Microsoft link.

Requirements:
(1) Microsoft .NET Framework Version 2.0,
(2) IE 5.01 or later
(3) Windows 2000; Windows Server 2003; Windows Vista; Windows XP

After download this exe you will get the additional option in your .net toolbox is Ajax Extensions.

Now two ways to use ajax
(1) Create a new ajax enabled web site. File->new ->ajax enabled web site
OR
(2) Changes made in your existing asp.net2.0 website.
You must add the following lines in your web.config files.
Add these lines in <system.web>
Code:
   1. </p>  
   2. <p><httpHandlers><br />  
   3. <remove verb=”*” path=”*.asmx”/><br />  
   4. <add verb=”*” path=”*.asmx” validate=”false” type=”System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/><br />  
   5. <add verb=”GET,HEAD” path=”ScriptResource.axd” type=”System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ validate=”false”/><br />  
   6. </httpHandlers><br />  
   7. <httpModules><br />  
   8. <add name=”ScriptModule” type=”System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″/><br />  
   9. </httpModules></p>  
  10. <p>
If you think why I add these lines in you web.config file then the answer is these lines you can find in you system

C or other system directory \Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025\web.config
If you don’t do that, then you will get the ‘sys is undefine’ javascript error. And your ajax will not work fine.

So, cheer up, now your application is ready to implement the ajax in .net 2.0. First place a “script manager� and then a “update panel�. Now Any control in the update panel if request to server, then only the part within the update panel will submitted to the server. This is very useful when your page is frequently postback.

Examples

Here I am giving you a very simple and useful example, which you deals many times while making the dynamic website. The example is when data is dynamically filled in the dropdownlist then when you choose the country and want to display the information about only that state which belongs to that particular country, means in another dropdown you want to fill the state related to that particular country, which is selected by the user, the page postback. To remove this postback you can use ajax as in the given example I did.

For the ease of your understanding I doesn’t uses anything on that particular page except the necessary thing and the things which are more convenient for your understandings. I use XML database (for you convenience) for this example, you can use any of other database.

My naming convention is as follows:
Drop down list for country: ddlCountry
Drop down list for state :ddlState
And a label :lblMsg

Now ddlCountry is dynamically filled by the xml database on pageload event. For this purpose I call a function fillCountry(). It is called only once to fill the dropdownlist ddlcountry.
Page Load Event:

Code:
<br />
”’ <summary><br />
”’ On page load the drop down for choose country is filled, by a function fillCountry()<br />
”’ </summary><br />
”’ <param name=”sender”></param><br />
”’ <param name=”e”></param><br />
”’ <remarks></remarks><br />
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
If Not IsPostBack Then<br />
fillCountry()<br />
End If</p>
<p>End Sub</p>
<p>
Sub routine fillCountry:
fillCountry() sub routine is used to fill the ddlCountry. It just read the xml file in the dataset. It uses listitem to fill the dropdownlist. Listitem text and listitem value is provided dynamically from the dataset.And at the end listitem is added to the dropdown.
Code:
<br />
”’ <summary><br />
”’ This subroutine is used to fill the drop down list for country , dynamically<br />
”’ </summary><br />
”’ <remarks></remarks><br />
Private Sub fillCountry()<br />
Dim ds As New DataSet()<br />
Dim lst As ListItem ‘list items that will be added to drop down list after assigning the text and value<br />
Dim i As Integer = 0<br />
If Not IsPostBack() Then<br />
ddlCountry.Items.Clear()<br />
End If</p>
<p>ds.ReadXml(Server.MapPath(”App_Data\country.xml”))<br />
lst = New ListItem()<br />
lst.Text = “–Choose Country–”<br />
lst.Value = “0″<br />
ddlCountry.Items.Add(lst)<br />
For i = 0 To ds.Tables(0).Rows.Count - 1<br />
lst = New ListItem()<br />
lst.Text = ds.Tables(0).Rows(i)(0)<br />
lst.Value = ds.Tables(0).Rows(i)(1)<br />
ddlCountry.Items.Add(lst)<br />
Next</p>
<p>End Sub</p>
<p>
The country drop down is dynamically filled by the above code
Code:
</p>
<p>On Select Index Change event of Country dropdownlist<br />
Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)<br />
ddlState.Enabled = True<br />
fillState(ddlCountry.SelectedItem.Value.ToString().Trim())<br />
lblMsg.Text = “You are selected ” + ddlCountry.SelectedItem.Text<br />
End Sub</p>
<p>
On the change of the dropdown selection, it enable the state dropdown list (ddlState) and dynamically fill the ddlstate dropdown by the function fillState(country’s Selected item value).

Sub routine fillState(CountryID):
If you have a close look on the below code you will find the line ddlState.Items.Clear()
Actually it is just clear the ddlState every time before dynamically filling the data into them, if this line is not written the each time when this function called it will not remove the previous data, just add new item into them.
Now again a new thing :
lst = New ListItem()
lst.Text = “–Choose State–”
lst.Value = “0″

What these three line do. These simply add the first list item as choose state so user can easily understand that this dropdown is to choose the state. And its value is 0 you can put it -1 or some thing string as you want.
One last thing
If ds.Tables(0).Rows(i)(2) = countryid Then
Above line is fillter the data for state which belongs to the particular countryid (See State.xml). If you are using another database you just do it by using where condition in the sql select statement.
Code:
</p>
<p>”’ <summary><br />
”’ It fills the drop down for the state dynamically<br />
”’ </summary><br />
”’ <param name=”countryid”&gtIt accepts the select item value from the country drop down list</param><br />
”’ <remarks></remarks><br />
Private Sub fillState(ByVal countryid As String)<br />
Dim ds As New DataSet()<br />
Dim lst As ListItem<br />
Dim i As Integer = 0<br />
ddlState.Items.Clear()<br />
lst = New ListItem()<br />
lst.Text = “–Choose State–”<br />
lst.Value = “0″<br />
ddlState.Items.Add(lst)<br />
ds.ReadXml(Server.MapPath(”App_Data\state.xml”))<br />
For i = 0 To ds.Tables(0).Rows.Count - 1<br />
If ds.Tables(0).Rows(i)(2) = countryid Then<br />
lst = New ListItem()<br />
lst.Text = ds.Tables(0).Rows(i)(0)<br />
lst.Value = ds.Tables(0).Rows(i)(1)<br />
ddlState.Items.Add(lst)<br />
End If<br />
Next</p>
<p>
Summary

Now we can come to this conclusion, you can enhance the lots of user interaction using Ajax. You can use it on every where, where you think it required. Another member for the toolbox in Ajax extension is for the different purpose like update progress is for the displaying the progress in the Ajax panel when it takes too much time to update. Another example of Ajax is you can use it in data grid also, it just remove the post back when you enable paging and shorting. In calendar control it also removes the post back etc. I will come with my other articles with more features of Ajax.

hacks is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 2 Users Say Thank You to hacks For This Useful Post:
a1_2cool@hotmail.com (08-02-2008), barnick (06-28-2008)
Click here to Donate to remove the Adverts.
Your Ad Here
Old 06-19-2008, 04:49 AM   #2 (permalink)
 
User Info
Join Date: Jun 2008
Age: 24
Achievements Posts: 8
Casino Cash: $850

Total Points: 506.04
Donate

Reputation: 10
flora2918 is on a distinguished road


Thank you , your advice will really be a help.

Last edited by flora2918; 06-19-2008 at 04:51 AM.
flora2918 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply
Click here to Donate to remove the Adverts.
Your Ad Here
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Securing Ajax Applications: Ensuring the Safety of the Dynamic Web KoOL Ebooks,tutorials & Other Stuff 0 12-04-2007 01:01 PM
Securing Ajax Applications: Ensuring the Safety of the Dynamic We ijhtio Ebooks,tutorials & Other Stuff 0 11-15-2007 07:36 PM
What is the easiest way to study for the Google dancergalny AdSense 0 08-29-2007 12:42 PM
how google implement gtalk in gmail like application anson Google 0 07-09-2007 09:38 AM
How can I implement Google Search in my... SASTRIJA JYOTHIS Google 0 07-07-2007 08:46 AM



Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles

RapidShare Links PhazeDDL Warez
PhazeDDL Warez