Solved

Autocomplete Control - Ajax

Posted on 2008-06-21
11
1,253 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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
 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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