Click here to Skip to main content
15,884,237 members
Articles / Web Development / ASP.NET
Article

Sample Code for Ajax in C#.net,Asp.net with Ajax.dll

Rate me:
Please Sign up or sign in to vote.
2.41/5 (19 votes)
6 Feb 20072 min read 123K   3.7K   21   12
This project is used to easily understand and implement Ajax in C#.net , ASP.Net with Ajax.dll
Sample image

Introduction

Ajax stands for Asynchronous Javascript and XML. Beginners may feel hard when they want to learn Ajax. They may got confused when they get into the samples like Atlas. This article is mainly for beginners of Ajax. The sample code is very easy and self-understandable.

Using The Code

Download the source code , unzip it and configure it in your IIS. The folder "Ajax_CSharp.Net" contains a sub-folder named "DB Table". This sub-folder contains a sql file. Execute the sql in your local sql server. A table with the name "employee" will be created. Add some more data into the table with the insert query in the sql file.

Execute the project Ajax_CSharp.net. Click the button. The button is a html button.It takes employee data from the table 'employee' without page post-back and populate the data in the dropdownlist. When a particular record is selected in the dropdownlist, the selected employee name and employee id willl be displayed in the label.

What is Ajax?

Ajax stands for Asynchronous Javascript and XML. It is a browser technology. This is an combination of Javascript and XML. The data is transfered in the form of XML. XMLHttp object is used in Ajax.

Why Ajax?

Ajax is used to eliminate round-trip to the server, when a request is posted to the server. In simple, we can say that when we want to access the database without page refresh, we can use Ajax.

Source Code

Page_Load()
C#
protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Ajax_CSharp));
    }

Get_Data()
JavaScript
[Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
    public string GetData()
    {
    //contains function to hit database
    }

Using Javascript

Javascript plays an vital role in Ajax. The server side function GetData() is a Ajax function. The function is coded with "[Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]" which shows that it is an Ajax function. This function is called from javascript in the click event of the button.

The server side function returns some value which is again handled by the javascript. HttpSessionStateRequirement.Readwrite is an optional argument which is used in order to use session variables inside ajax function. One thing we have to remember is that we can't access server side controls inside the GetData() function. Thats why we are returning the data to the javascript and populate dropdownlist from the javascript.

JavaScript
function GetData()
    {
        var response;
        Ajax_CSharp.GetData(GetData_CallBack);    
    }
    
    function GetData_CallBack(response)
    {
        var response=response.value;
     // code to handle response value
        
     }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
jeevav10-Aug-10 19:11
jeevav10-Aug-10 19:11 
GeneralSuperrrrr man, kazhakal kazhai...india va kokka....mmmuack Pin
vsstephen27-Jul-10 18:35
vsstephen27-Jul-10 18:35 
GeneralGood start Pin
Waruna3-Oct-09 17:56
Waruna3-Oct-09 17:56 
GeneralNeeds more Pin
fly90417-Mar-09 6:23
fly90417-Mar-09 6:23 
QuestionNot working for Firefox Pin
Member 83707622-Jan-09 7:19
Member 83707622-Jan-09 7:19 
GeneralCould not find the project file Pin
HemJoshi10-Sep-08 22:22
HemJoshi10-Sep-08 22:22 
GeneralRegarding Ajax drop down with Text box Pin
Mani Kumar21-Feb-08 21:37
Mani Kumar21-Feb-08 21:37 
GeneralHelps beginners Pin
webqa30-Apr-07 19:14
webqa30-Apr-07 19:14 
GeneralIt's Useful Pin
Florence Juliet J9-Feb-07 18:44
Florence Juliet J9-Feb-07 18:44 
GeneralWhere is the javascript Pin
Not Active7-Feb-07 2:26
mentorNot Active7-Feb-07 2:26 
GeneralRe: Where is the javascript Pin
Bala Murali S8-Feb-07 19:59
Bala Murali S8-Feb-07 19:59 
GeneralRe: Where is the javascript Pin
Not Active9-Feb-07 2:23
mentorNot Active9-Feb-07 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.