Solved

Autocomplete Control - Ajax

Posted on 2008-06-21
11
1,238 Views
Last Modified: 2012-06-27
I need help this AutoComplete Control in asp.net. It does not call Webservice. Nothing happened when typed in the textbox1. Help.


<cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" TargetControlID="TextBox1" <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="Test_test" title="Untitled Page" %>

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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/WebService.asmx" />
             </Services>
        </asp:ScriptManager>
    </p>
    <p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   
        <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
            DelimiterCharacters="" Enabled="True" ServicePath="" TargetControlID="TextBox1" ScriptPath="~/webservice.asmx" ServiceMethod="GetmyData" MinimumPrefixLength="2" CompletionSetCount="12" EnableCaching ="true">
        </cc1:AutoCompleteExtender>
   
   
    <p>
        <asp:Button ID="Button1" runat="server" Text="Button" />

   
   
</asp:Content>
        </cc1:AutoCompleteExtender>



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


' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
 

    Public Function GetmyData(ByVal Prefixtext As String) As String

        Dim myConstr As String = "mystring"
        Dim myCon As New SqlConnection(myConstr)
        Dim mySelectstr As String = "select *from mytable where type like@prefixtext"
        Dim DA As New SqlDataAdapter(mySelectstr, myCon)

        DA.SelectCommand.Parameters.Add("@prefixtext", SqlDbType.VarChar, 50).Value = Prefixtext & "%"
        Dim dt As New DataTable
        DA.Fill(dt)

        Dim items() As String = New String(dt.Rows.Count) {}
        Dim i As Integer = 0
        Dim dr As DataRow
        For Each dr In dt.Rows
            items.SetValue(dr("id").ToString(), i)
            i += 1


        Next


        Return items.ToString



    End Function

End Class
0
Comment
Question by:VBdotnet2005
  • 5
  • 5
11 Comments
 
LVL 11

Expert Comment

by:anuragal
ID: 21840271
Hi,

Try change in your defination of

<cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
            DelimiterCharacters="" Enabled="True" ServicePath="" TargetControlID="TextBox1" ScriptPath="~/webservice.asmx" ServiceMethod="GetmyData" MinimumPrefixLength="2" CompletionSetCount="12" EnableCaching ="true">
        </cc1:AutoCompleteExtender>

from ScriptPath="~/webservice.asmx" to ServicePath="WebService.asmx"

If it doesn't work then try these links to use this control

http://www.aspdotnetcodes.com/AutoComplete_From_Database.aspx
Or
http://dn.codegear.com/article/38250

Anurag
0
 

Author Comment

by:VBdotnet2005
ID: 21843545
0
 
LVL 13

Expert Comment

by:sm394
ID: 21844448
0
 

Author Comment

by:VBdotnet2005
ID: 21848991
I watched this video before, many time, still could not it  to work. Any thoughts......
0
 

Author Comment

by:VBdotnet2005
ID: 21849112
How can I test if Webservice is called?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:sm394
ID: 21853328
simply download the code attached with this video and run it.
Testing of webservice is done in aspx: i.e. ServiceReference Path="AutoComplete.asmx
and   ServicePath="AutoComplete.asmx"
                ServiceMethod="GetCompletionList"
//=======================================================================
ASPX Code Ref: http://www.asp.net/learn/ajax-videos/video-122.aspx
//=======================================================================

 <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
        <asp:ServiceReference Path="AutoComplete.asmx" />
        </Services>
        </asp:ScriptManager>
        <div>
        <p/>
            Type some characters in this textbox.  The web service returns random words
            that start with the text you have typed.
            <br /><br />
           
            <asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
            <ajaxToolkit:AutoCompleteExtender
                runat="server"
                ID="autoComplete1"
                TargetControlID="myTextBox"
                ServicePath="AutoComplete.asmx"
                ServiceMethod="GetCompletionList"
                MinimumPrefixLength="2"
                CompletionInterval="1000"
                EnableCaching="true"
                CompletionSetCount="12" />
    </div>
    </form>  
0
 
LVL 13

Expert Comment

by:sm394
ID: 21853348
also check the CompletionInterval
Try different values i.e  1 to 1000

CompletionInterval="50"

0
 

Author Comment

by:VBdotnet2005
ID: 21891363
Please see the following code. I still can't get to work when typed in textbox1 Thank you

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="Test_test" title="Untitled Page" %>

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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/webService.asmx" />
             </Services>
        </asp:ScriptManager>
    </p>
    <p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   
        <cc1:AutoCompleteExtender
            ID="TextBox1_AutoCompleteExtender"
            runat="server"
            DelimiterCharacters=""
            Enabled="True"  
            TargetControlID="TextBox1"
            ServicePath="~/webService.asmx"
            ServiceMethod="GetmyData"
            MinimumPrefixLength="2"
            completioninterval="50"     > tried dif #, not working for me
            EnableCaching ="true"
            CompletionSetCount="12">
           
        </cc1:AutoCompleteExtender>
   
   
    <p>
        &nbsp;</asp:Content>




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


' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
    Inherits System.Web.Services.WebService


    <WebMethod()> _
    Public Function GetmyData(ByVal prefixtext As String) As String

        Dim myConstrCount As String = "myconstring"
        Dim myConCount As New SqlConnection(myConstrCount)
        Dim mySelectstrCount As String = "select AccountID from mytable where AccountID like@prefixtext"
        Dim DA As New SqlDataAdapter(mySelectstrCount, myConCount)

        DA.SelectCommand.Parameters.Add("@prefixtext", SqlDbType.VarChar, 50).Value = prefixtext & "%"
        Dim dt As New DataTable
        DA.Fill(dt)

        Dim items() As String = New String(dt.Rows.Count) {}
        Dim i As Integer = 0
        Dim dr As DataRow
        For Each dr In dt.Rows
            items.SetValue(dr("id").ToString(), i)
            i += 1


        Next


        Return items.ToString

    End Function

End Class
0
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 21892318
below is working code snippet change the con string and query with yours

Hope that helps


//===========================================================

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

     

        <cc1:AutoCompleteExtender ID="txtSearch_AutoCompleteExtender" runat="server" DelimiterCharacters=""

            Enabled="True" CompletionInterval="1" MinimumPrefixLength="1" ServiceMethod="GetAuto"

            ServicePath="WebService.asmx" TargetControlID="TextBox1">

        </cc1:AutoCompleteExtender>
 

===============================================

<WebMethod()> _

    Public Function GetAuto(ByVal prefixText As String, ByVal count As Integer) As String()

        If (count = 0) Then

            count = 10

        End If

        Dim items As New List(Of String)

        
 

        Dim con As SqlConnection = New SqlConnection("Data Source=pc1;Initial Catalog=dbLib; Integrated Security=True")

        Dim cmd As SqlCommand = New SqlCommand("SELECT title as result from tbl_online_resources where title like '%'+REPLACE(@search, ' ', '%')+'%'", con)

        cmd.Parameters.Add(New SqlParameter("@search", SqlDbType.NVarChar, 255))

        cmd.Parameters("@search").Value = prefixText

        con.Open()

        Dim reader As SqlDataReader = cmd.ExecuteReader

        Dim i As Integer = 0
 

        While (reader.Read AndAlso (i < count))

            items.Add(reader("result").ToString)

            i = (i + 1)
 

        End While

        con.Close()

        Return items.ToArray
 

    End Function

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 21958640
I have a question on this function. Public Function GetAuto(ByVal prefixText As String, ByVal count As Integer) As String()when I type on textbox1, I just need to type a letter or letters, correct?
0
 
LVL 13

Expert Comment

by:sm394
ID: 21962704
you can set it by changing the value of  MinimumPrefixLength="1"  
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now