Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Autocomplete Control - Ajax

Posted on 2008-06-21
11
Medium Priority
?
1,296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 11

Expert Comment

by:Anurag Agarwal
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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