Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have given code for Auto Complete Extender but i'm not getting why its not working.

.aspx file

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Book_Search.Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
   <asp:ScriptManager ID="a" runat="Server" EnablePageMethods="true">
   </asp:ScriptManager>
   <div>
       <asp:TextBox ID="txt" runat="Server" AutoPostBack="True"
           ontextchanged="txt_TextChanged" ></asp:TextBox>
       <asp:AutoCompleteExtender FirstRowSelected="true"
       ServiceMethod="GetTitle"
       MinimumPrefixLength="1"
       CompletionInterval="1000"
       EnableCaching="true"
       CompletionSetCount="2"
       TargetControlID="txt"
       ID="AutoCompleteExtender1" runat="server">
       </asp:AutoCompleteExtender>
       <br />
       <br />
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   </div>
</form>
</body>
</html>


Source code file .cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;



namespace Book_Search
{
    public partial class Default : System.Web.UI.Page
    {
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Script.Services.ScriptMethod]
        [System.Web.Services.WebMethod]
        public string[] GetTitle(string prefixText, int count)
        {
            MySqlConnection con = new MySqlConnection(@"server=localhost;user id=root;password=tushar;database=library");
            dt = new DataTable();
            DataSet ds = new DataSet();
            try
            {
                string query = @"Select Title From library.demo d Where Title like '" + prefixText + "%';";
                con.Open();
                MySqlCommand cmd = new MySqlCommand(query, con);
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                //con.Close();
            }
            dt = ds.Tables[0];

            List<string> txtItems = new List<string>();
            string dbValues;

            foreach (DataRow dr in dt.Rows)
            {
                dbValues = dr["Title"].ToString();
                txtItems.Add(dbValues);
            }

            return txtItems.ToArray();
        }
    }
}
Posted
Updated 24-May-12 10:20am
v2
Comments
[no name] 24-May-12 8:39am    
You need to explain more. What isn't working? Is the server method not being called? Are the results returned?
Sandeep Mewara 24-May-12 8:41am    
'Not working' is less an info to suggest anything.
Jim Jos 24-May-12 9:13am    
You need to have a separate webservice .asmx file right... Here you have put it under Default.class file which is code behind.. Please confirm you have a separate webservice..

Refer my article hope it helps
Using Ajax AutoCompleteExtender for autosuggest[^]
 
Share this answer
 
XML
<ajaxToolkit:AutoCompleteExtender
    runat="server"
    ID="autoComplete1"
    TargetControlID="myTextBox"
    ServiceMethod="GetCompletionList"
    ServicePath="AutoComplete.asmx"
    MinimumPrefixLength="2"
    CompletionInterval="1000"
    EnableCaching="true"
    CompletionSetCount="20">
   
</ajaxToolkit:AutoCompleteExtender>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900