Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
4.60/5 (4 votes)
See more:
Hello, Can anyone please help me out with the autocomplete textbox code, Which uses c# and Ajax. I'm very much confused of developing it.
Posted
Comments
Arjun YK 7-Jun-12 7:29am    
Use this link for the solution http://www.dotnetfunda.com/articles/article224.aspx. and just replace the Script Manager tool with Tool Script Manager. Enjoy The Output......May be u have to change the like Clause also if needed in my case i used it as '@Name'

Hi
you can also use this jQuery http://jqueryui.com/demos/autocomplete/[^] plugin along with a web service
 
Share this answer
 
This is my aspx File
XML
<asp:ImageButton ID="frumgrp_srch_imgbtn" runat="server" ImageUrl="~/images/search.png" />
                    &nbsp;&nbsp;&nbsp;&nbsp;<br />
                    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="frumgrp_srch_txt"
                                 EnableCaching="true"
       BehaviorID="AutoCompleteCities"
       ServiceMethod="GetCities" ServicePath="~/AutoComplete.asmx"  MinimumPrefixLength="1"      CompletionSetCount="5"
        FirstRowSelected="true">
        </asp:AutoCompleteExtender>


This Is My Cs File
C#
using System;
using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

[WebService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : WebService
{
    public AutoComplete()
    {
    }

    [WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        if (count == 0)
        {
            count = 10;
        }
        DataTable dt = GetRecords(prefixText);
        List<string> items = new List<string>(count);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string strName = dt.Rows[i][0].ToString();
            items.Add(strName);
        }
        return items.ToArray();
    }

    public DataTable GetRecords(string strName)
    {
        string strConn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@Name", strName);
        cmd.CommandText = "Select Name from Test where Name like '%'+@Name+'%'";
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        con.Open();
        dAdapter.Fill(objDs);
        con.Close();
        return objDs.Tables[0];





    }
}



This is My asmx File
<![CDATA[<%@ WebService Language="C#" CodeBehind="~/App_Code/AutoComplete.cs" Class="AutoComplete" %>

I'm not able to execute this code. Infact the webservice it self is not getting called. Can Anyone help. Your help will be a great relief to me.
Eagerly Waiting For The Solution
Thanks in advance
 
Share this answer
 
v3
Comments
Member 10337869 26-Jan-14 23:49pm    
your in ServiceMethod="GetCities" is not present in cs file .It should be GetCompletionList
I Know you already got the answer from all previous posts,
But here am providing you easy way to know about the Ajax Autocomplete control

Videos:

www.youtube.com/watch?v=2i50R-fHHlA[^]
www.youtube.com/watch?v=nYac4sxChdk[^]


Thanks
--RA
 
Share this answer
 
Comments
Arjun YK 8-Jun-12 6:15am    
Thank U
Rajesh Anuhya 8-Jun-12 7:42am    
Vote or Accept Answer if it's helpful to you
You can either use XMLHttpRequest or using AJAX controls.

Below are demo for AJAX control AutoCompleteExtender:

Using Ajax AutoCompleteExtender for autosuggest[^]

XMLHTTPRequest:

Google-Like-Search-TextBox
 
Share this answer
 
v2
Visit The Following link you will be getting your problem solutions.Using Ajax AutoCompleteExtender for autosuggest[^]
 
Share this answer
 
Autucomplete without using web service It should help you.-->Test.aspx(html code)
XML
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
    <table>
        <tr>
            <td>
            <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
                <asp:AutoCompleteExtender ID="txtContactsSearch_AutoCompleteExtender"
                    runat="server" ServiceMethod="Getdata"  MinimumPrefixLength="1" CompletionInterval="1000"
                    EnableCaching="true" CompletionSetCount="1"
                    TargetControlID="txtContactsSearch" UseContextKey="True" >
                    </asp:AutoCompleteExtender>
            </td>
        </tr>
    </table>
    </div>
    </form>


Code behind -->Test.aspx.cs

C#
[System.Web.Services.WebMethodAttribute(),System.Web.Script.Services.ScriptMethodAttribute()]
    public static List<string>  Getdata(string prefixText, int count, string contextKey)
    {
       //Make your database connection here
       string strSQL = "SELECT * FROM YourTable WHERE coloumnName Like '" + prefixText + "%'";
       //Get data in datatable 

        List<String> list = new List<String>();
        foreach(DataRow dr in dataTable.Rows)
        {
            list.Add(dr["coloumnName "].ToString());
        }
        return list;
    }
 
Share this answer
 
v3
You can use XMLHttpRequest object[^]
also,
You can use jQuery for same.
Plugins/Autocomplete[^]
UI/API/1.8/Autocomplete[^]
 
Share this answer
 
Use this link for the solution http://www.dotnetfunda.com/articles/article224.aspx. and just replace the Script Manager tool with Tool Script Manager. Enjoy The Output......May be u have to change the like Clause also if needed in my case i used it as '+@Name+'
 
Share this answer
 
v3
Hi

Make sure that the function / or Service method lies in Aspx page, not in a control of course.
 
Share this answer
 
Comments
nika2008 13-Jun-13 11:56am    
check data before you give Solution

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



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