Implementing AJAX in asp.net application using Ajax.dll

Problem: We have a dropdown server control on our web page that we want to populate with users name from the user table in the database when user clicks on the populate button without flickering effect on the web page.

Follow these steps for implementing ajax solution to this problem in your web application using ajax.dll

1. Download the latest version of ajax.dll from the following link:

http://ajax.schwarz-interactive.de/csharpsample/default.aspx

2. Add the httphandler for ajax in the web.config file of your web app.
Example-
< httpHandlers >
< add verb="POST, GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
< /httpHandlers >

3. Register the aspx.cs class with the ajax.
Exlample-
protected void Page_Load(object sender, EventArgs e)
{

Ajax.Utility.RegisterTypeForAjax(typeof(AjaxDemo));

}

AjaxDemo -is the class name.


4. Now create the serverside ajax method in the .aspx.cs file.
Example-

[Ajax.AjaxMethod]
public DataSet GetUsers()
{
IBLLService ObjBLLService = BLLServiceFactory.GetBLLService();
DataSet Ds=ObjBLLService.GetData();
return Ds;
}
In the GetUsers method, Replace the existing code with your own code to get the users name data from the DB.

5.Write following java script on the .aspx page.

< script language="javascript" type="text/javascript" >

function getusername()
{
AjaxDemo.GetUsers(fillusers);
}
function fillusers(Response)
{
if(Response!=null)
{
var tab=Response.value.Tables[0];
var user=document.getElementById('<%=ddlUsers.ClientID%>');
for(i=0;i < tab.Rows.length;i++)
{
var ro=tab.Rows[i];
user.options[user.options.length]=new Option(ro.firstname,i); // firstname is the field name in the DB table.
}
}
}
< /script >

Add dropdown control and Pupulate button on the web page.

User Name : < asp:DropDownList ID="ddlUsers" runat="server" Width="100%" >

< input id="btnFill" onclick="getusername()" type="button" value="Fill Users using Ajax dll" />

6. We are finished with the coding now its time to test it.

4 comments:

Anonymous said...

Do we need to take any extra care when we want to run this code on shared hosting where we dont need to keep ajax.dll in our bin folder.

Unknown said...

ya buddy, but i it working on the local but when i am upload it on the server it gives java script error: "AjaxDemo is not define." if any idea then plz publish it!!!!!!!
thanks.

Unknown said...

hi every ony
am using ajax dll in .net 2.0 framework when i shifted my project to windows 2008 .net 3.5 framework ajax dll not working function name not found error(javascript) error throwing how can i use ajax dll in 3.5 any upgrade

gaurav said...

@jay
you have to change the IIS pooling settings from default pooling to classic .Net pooling