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
Solved

Autocomplete Control - Ajax

Posted on 2008-06-21
11
1,259 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: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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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