Archive for the 'Ajax' Category

Using Ajax Auto-complete extender control

Now its easy to use auto-complete or auto-suggest text box in your asp.net project or website. It’s quite simple. Follow the steps carefully.

Step 1: Open visual studio

Step 2: Select a ajax enabled webapplication or ajax enabled website

Step 3: In the design view of default.aspx page, make sure script manager is in the page and if not add a script manager from the ajax tool controls

Step 4: Drag and drop the textbox and autocomplete extender control and rename textbox as mytextbox.

Step 5: Copy the access database you want to use to app_data folder of the website

Step 6: Right click on the app_data folder and then add existing item and add the mdb file and you can see the database appear in the application folder.Right click on the project->Add new item ->add a webservice and rename it as autocomplete.asmx.
( In case this is the first time you are creating a webservice you may get a prompt asking if you need to put the code in a separate class file. Select Yes )

Step 7: Now paste the code in autocomplete.cs file under the app_code folder. Change the query string and other credentials as applicable to your project

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : WebService
{
[WebMethod]
public string[] GetCompletionList(string prefixText,int count, string contextKey)
{
count = 10;
string querystring;
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/user/My Documents/Visual Studio 2008/Projects/WebApplication1/WebApplication1/App_Data/data.mdb");
querystring = "select distinct student_name from student_query where course_name like @prefixText";
OleDbDataAdapter da = new OleDbDataAdapter(querystring, aConnection);
da.SelectCommand.Parameters.Add("@prefixtext", OleDbType.VarChar, 50).Value = prefixText + "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr[0].ToString(), i);
i++;
}
return items;
}
}

Step 8: In the default.aspx page make the following changes
For the autocomplete extender include the following properties

TargetControlID="myTextBox" ServicePath="AutoComplete.asmx" ServiceMethod="getCompletionlist" MinimumPrefixLength="1" UseContextKey="true" ContextKey="course">

Step 9 : Now if you run the project you should be able to see the suggestions for a particular letter depending on the words in your database.

Tips: If you have done all the above steps are still you are not able to find what is wrong with your project , there should be something wrong with your config file.
You don’t have to give up. Follow these steps now,
Go to this page
Watch the video if you are new and then download the c# code from there. Make necessary modification according to your project as you already have the webservice code to connect to access database. With little modification of the query strings you can use SQL datasource too.

Comments are welcome. Please let me know if this works for you.

Add Ajax to your ASP.Net Project

There are lots of ajax libraries which can be used to implement ajax in any website or any web project. Anthem.net is a famous opensource Ajax library which can be used to implement ajax functionalities to an ajax enabled website.

Visual Studio 2008 provides in build libraries for Ajax support.  Ajax control toolkit has to be downloaded and installed in the case of VS2005 and VS2008 bypass this step.

Various sample functions of Ajax can be found here to take a look before starting with the actual coding.

Download Ajax control toolkit. (AjaxControlToolkit-NoSource.zip would be fine if you are just going to use and Ajax controls)

Once it has been installed properly you would be able to see ASP.Net Ajax-Enabled Web Application in the New project menu.


Download ASP.Net Ajax Extensions for using the Ajax controls to your project.

Ajax Extensions can be installed with the help of this video and after installing you should be able to see the tools in your IDE.

Let me explain the steps to connect an Ajax Autocomplete extender control with an access database in the later post. This can be done by writing a webservice to connect to the database.