Solved

Autocomplete Control - Ajax

Posted on 2008-06-21
11
1,268 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
Independent Software Vendors: 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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