how do i autocomplete in ajax?

Last post 04-27-2008 5:17 AM by media_1408. 7 replies.

Sort Posts:

  • how do i autocomplete in ajax?

    09-02-2007, 8:54 AM
    • Member
      52 point Member
    • guysch
    • Member since 08-10-2007, 5:49 PM
    • Posts 70

    hi every1 i just upgraded from atlas to ajax 2.0

    it wasnt to hard but i have a problem i used the <atlas:autocomplete

    tag in atlas

    but it wasnt like the other tags i cant just cahnge the atlas to asp:

    cause it dosnt have it

    how can i do it then?

    any1 know?

     plz help tnx a lot

    here to learn :)
  • Re: how do i autocomplete in ajax?

    09-02-2007, 2:08 PM
    Answer
    • Member
      55 point Member
    • media_1408
    • Member since 08-26-2007, 3:36 PM
    • Islamabad
    • Posts 54

     Hi, you can easily implement auto completion in ajax using AJAX AutocompleteExtender, the sample code is shown below.

     .aspx page

      <asp:TextBox ID="txt_search_city" Width="130px" runat="server"> 

      <cc1:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="txt_search_city"
                                ServiceMethod="GetCompletionListWithContext" UseContextKey="true" MinimumPrefixLength="2"
                                EnableCaching="false" CompletionSetCount="20" CompletionInterval="2000" FirstRowSelected="true"
                                DelimiterCharacters=";," /> 

     

    And in Code Behind - aspx.vb 

     <System.Web.Services.WebMethod()> _
          <System.Web.Script.Services.ScriptMethod()> _
          Public Shared Function GetCompletionListWithContext(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
            If Not contextKey = Nothing Then
                Return "this is a total of fifteen words that will be returned when we use context".Split(" ")
            End If
            If count = 0 Then
                count = 20
            End If
            Dim items As New System.Collections.Generic.List(Of String)
            'get data from database
            Dim _loc As New Locations
            Dim str As String
            Dim ds As DataSet = _loc.Load_Cities(prefixText, count)
            If ds.Tables(0).Rows.Count > 0 Then
                Dim i As Integer
                For i = 0 To count - 1
                    str = ds.Tables(0).Rows(i).Item("city").ToString()
                    items.Add(str)
                Next
            End If
            Return items.ToArray()
        End Function

    This will help you implementing auto completion in ASP.NET 2.0 using AJAX Toolkit

     

    >> Web Developer
    Filed under:
  • Re: how do i autocomplete in ajax?

    09-02-2007, 2:43 PM
    • Member
      52 point Member
    • guysch
    • Member since 08-10-2007, 5:49 PM
    • Posts 70

     hi tnx for the reply

     
    but can you maybe tell me how do i do it in c#?

     


     and what are these two?

     <System.Web.Services.WebMethod()> _
          <System.Web.Script.Services.ScriptMethod()>

     where do i put stuff like that?

     

    please help

    tnx
     

    here to learn :)
  • Re: how do i autocomplete in ajax?

    09-02-2007, 3:17 PM
    Answer
    • Member
      55 point Member
    • media_1408
    • Member since 08-26-2007, 3:36 PM
    • Islamabad
    • Posts 54

     Hi,

     AJAX Toolkit Autocomplete Extender use Webservice Method for fetching records and display in auto popup list, when user type some text in textbox. therefore you must create web method with same definition as describe in ajax autocomplete documentation.

    If you want not to create separate webservice page, instead of it define method in normal page code behind, then you must first, import the following namespace.

     
    using System.Web.Services;

    using System.Web.Script.Services;

     

    then in code  create a method as shown below.

     [WebMethod]
     public string[] GetCompletionList(string prefixText, int count)
      {

        '// rest define in another post 

     } 

     Thanks.

     
     


     

    >> Web Developer
    Filed under:
  • Correct My Autocomplete extenders webservice!

    02-13-2008, 3:49 AM

    while i run the program i find webservice method as error reply and asks me to invoke! could u correct my code here to run autocomplete,and the another thing i could'nt get the popup list wats wrong in this code Help me

    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Collections.Generic
    Imports System.Configuration

    <WebService(Namespace:="http://microsoft.com/webservices/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <System.Web.Services.WebMethod()> _
    '<System.Web.Script.Services.ScriptMethod()> _
    '<System.Web.Script.Services.ScriptService()> _
    Public Class AutoComplete
        Inherits System.Web.Services.WebService

        <WebMethod()> _
    Public Shared Function GetName(ByVal prefixtext As String, ByVal count As Integer)
            Dim items As New List(Of String)
            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            'Dim Reader As SqlDataReader = cmd.ExecuteReader
            Dim adapter As SqlDataAdapter

            Dim dbquery As String
            Try

                con.Open()
                con = New SqlConnection("Data Source=localhost;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True")
                dbquery = "Select Name from dbAutoComplete WHERE Name LIKE '" & prefixtext & "%'"""

                cmd = New SqlCommand(dbquery, con)
                adapter = New SqlDataAdapter(dbquery, con)
                Dim dt As DataTable
                dt = New DataTable()
                adapter.Fill(dt)
                Dim iCount As Integer
                Dim sItems As New List(Of String)
                Dim drRow As DataRow

                For Each drRow In dt.Rows
                    sItems.Add(drRow("
    Name").ToString())
                    iCount = iCount + 1
                Next

                

                Return
    sItems.ToArray()
            Catch e As Exception

                Throw e

            Finally
            End Try


            'cmd.ExecuteNonQuery()
            'While Reader.Read
            '    Dim name As String = CStr(Reader("Name"))
            '    items.Add(name)
            'End While

            'Return items.ToArray
            con.Close()
        End Function


    End Class

     
    udaya karthick
  • Re: Correct My Autocomplete extenders webservice!

    02-13-2008, 4:55 AM
    • Member
      55 point Member
    • media_1408
    • Member since 08-26-2007, 3:36 PM
    • Islamabad
    • Posts 54

     Hi, i used Autocomplete in many projects and its working fine, recently i used in one of my web application.

     I used in VB.NET just convert the code to c# as i have very limited time.

    Create a web service : any name

     Below is complete code that i used for retrieving city names from database based on query

    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.Data

    <WebService(Namespace:="http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    <System.Web.Script.Services.ScriptService()> _
    Public Class WebService
        Inherits System.Web.Services.WebService

        <WebMethod()> _
      Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As String()
            If count = 0 Then
                count = 20
            End If
            Dim items As New System.Collections.Generic.List(Of String)
            Dim i As Integer = 0
            Dim _loc As New Locations
            Dim str As String
            Dim ds As DataSet = _loc.Load_Cities(prefixText, count)
            If ds.Tables(0).Rows.Count > 0 Then
                For i = 0 To ds.Tables(0).Rows.Count - 1
                    str = ds.Tables(0).Rows(i).Item("city").ToString()
                    If ds.Tables(0).Rows(i).Item("StateProvince").ToString() = "XX" Then
                        str = ds.Tables(0).Rows(i).Item("city").ToString() & " - " & ds.Tables(0).Rows(i).Item("CountryName").ToString()
                    Else
                        str = ds.Tables(0).Rows(i).Item("city").ToString() & " - " & ds.Tables(0).Rows(i).Item("StateProvince").ToString()
                    End If
                    items.Add(str)
                Next
            End If
            Return items.ToArray()
        End Function

    End Class

    On front end just use as shown below.

     

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

     

      <asp:TextBox ID="txt_cityname" ToolTip="Type in your city name and a list will appear.  Select your city and click Go!"
                                autocomplete="off" runat="server" Width="180px"></asp:TextBox>
                            <ajaxToolkit:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender3" runat="server"
                                TargetControlID="txt_cityname" WatermarkText="Enter City Name" />
                                  <ajaxToolkit:AutoCompleteExtender runat="server" BehaviorID="AutoCompleteEx" ID="autoComplete1"
                                TargetControlID="txt_cityname" ServicePath="WebService.asmx" ServiceMethod="GetCompletionList"
                                MinimumPrefixLength="1" CompletionInterval="1000" EnableCaching="true" CompletionListCssClass="autocomplete_completionListElement"
                                CompletionListItemCssClass="autocomplete_listItem" CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
                                DelimiterCharacters=",">
                                <Animations>
                        <OnShow>
                            <Sequence>
                                <%-- Make the completion list transparent and then show it --%>
                                <OpacityAction Opacity="0" />
                                <HideAction Visible="true" />
                               
                                <%--Cache the original size of the completion list the first time
                                    the animation is played and then set it to zero --%>
                                <ScriptAction Script="
                                    // Cache the size and setup the initial size
                                    var behavior = $find('AutoCompleteEx');
                                    if (!behavior._height) {
                                        var target = behavior.get_completionList();
                                        behavior._height = target.offsetHeight - 2;
                                        target.style.height = '0px';
                                    }" />
                               
                                <%-- Expand from 0px to the appropriate size while fading in --%>
                                <Parallel Duration=".4">
                                    <FadeIn />
                                    <Length PropertyKey="height" StartValue="0" EndValueScript="$find('AutoCompleteEx')._height" />
                                </Parallel>
                            </Sequence>
                        </OnShow>
                        <OnHide>
                            <%-- Collapse down to 0px and fade out --%>
                            <Parallel Duration=".4">
                                <FadeOut />
                                <Length PropertyKey="height" StartValueScript="$find('AutoCompleteEx')._height" EndValue="0" />
                            </Parallel>
                        </OnHide>
                                </Animations>
                            </ajaxToolkit:AutoCompleteExtender>

     

    That's it, test and see output.

     

    Also note that AutocompleteExtender is not working on .ascx files, you must use that in .aspx file directly otherwise it will not work.

     

    Please let us know if you require more help. 

     

     

     

     

     



     

    >> Web Developer
  • Re: Correct My Autocomplete extenders webservice!

    04-27-2008, 3:45 AM
    • Member
      31 point Member
    • Musafiir
    • Member since 04-05-2008, 4:13 AM
    • Posts 69

    Hi, am searching a functioning code for autocomplete textbox from database. I studied yours. But whats the importance of all the code in the Animation tag? if removed, how will it affect the performance of the applicaiton?

     Thanks for answers..... :-)
     

  • Re: Correct My Autocomplete extenders webservice!

    04-27-2008, 5:17 AM
    • Member
      55 point Member
    • media_1408
    • Member since 08-26-2007, 3:36 PM
    • Islamabad
    • Posts 54

    Hi,

    Animation is used just to make auto complition list rich and attractive, for performance point of its not good, if you are developing a scalable web application then try to avoid animation scripts.

     
     

    >> Web Developer
Page 1 of 1 (8 items)