Categories

Tools

bitdefender

1&1 Web Hosting

Example of how to use Ajax control AutoCompleteExtender associated with an Access database


"AutoComplete is an ASP.NET AJAX extender that can be attached to any TextBox control, and will associate that control with a popup panel to display words that begin with the prefix typed into the textbox".

This example shows how to use this control, in a real-life example. When you start typing a word in the textbox below appears the list of cities that begin with those letters. If you select one of the cities on the list, the text will be pasted into the textbox.

Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoCompleteExtenderDemo.aspx.cs" Inherits="AutoCompleteExtenderDemo" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Example of how to use Ajax control AutoCompleteExtender associated with an Access database - MdmSoft</title>
    <style type="text/css">
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ToolkitScriptManager ID="tsmCity" runat="server"></asp:ToolkitScriptManager>
            <asp:TextBox ID="TextBoxCity" runat="server" Height="18px" Width="300px"></asp:TextBox>
            <asp:AutoCompleteExtender
                runat="server"
                ID="AutoCompleteExtenderCity"
                TargetControlID="TextBoxCity"
                ServiceMethod="GetCompletionList"
                MinimumPrefixLength="1"
                CompletionInterval="500"
                EnableCaching="true"
                CompletionSetCount="15"
                UseContextKey="True"
                ShowOnlyCurrentWordInCompletionListItem="true">
            </asp:AutoCompleteExtender>
        </div>
    </form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Web;

public partial class AutoCompleteExtenderDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// This method returns an array with the list of cities that begin with prefixText
    /// </summary>
    /// <param name="prefixText"></param>
    /// <param name="count"></param>
    /// <param name="contextKey"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        List<String> cities = GetCities(prefixText, count);
        return cities.ToArray();

    }

    /// <summary>
    /// This method queries a database and returns a list of cities that begin with prefixText
    /// </summary>
    /// <param name="prefixText"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    private static List<string> GetCities(string prefixText, int count)
    {
        // Define the connection string
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                                    HttpContext.Current.Server.MapPath("~/App_Data/Northwind.mdb");

        // Define the SELECT command
        string selectCommand =  "SELECT DISTINCT TOP " + count + " City " +
                                "FROM Customers " +
                                "WHERE City LIKE '" + prefixText + "%' " +
                                "ORDER BY City";

        List<String> suggestedCities = new List<string>();
        DataTable dtSuggestedCities = new DataTable();

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();

            using (OleDbCommand command = new OleDbCommand(selectCommand,connection))
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                adapter.Fill(dtSuggestedCities);
            }
        }

        if (dtSuggestedCities != null
            && dtSuggestedCities.Rows != null
            && dtSuggestedCities.Rows.Count > 0)
        {
            foreach (DataRow dr in dtSuggestedCities.Rows)
            {
                suggestedCities.Add(dr["City"].ToString());
            }
        }

        return suggestedCities;
    }
}

 

Result from the example:

Example of how to use Ajax control AutoCompleteExtender associated with an Access database

Posted in Ajax by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.