Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use Visual Studio 2008 and AjaxToolKit 3.5 to accomplish auto-complete TextBox but inpitee of all that I have done, even though I'm not recieving any error message when the program is run, my AutoComplete TextBox is not working. Below are the various codes in ASP.NET and VB.NET. I'd be glad if someone could help me out.



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Subjects.aspx.vb" Inherits="Subjects" %>

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

<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI" tagprefix="asp" %>


HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Subject Master</title>
    <style type="text/css">
        .style2
        {
        }
        .style4
        {
            width: 297px;
        }
        .style6
        {
            width: 268px;
        }
        .style3
        {
            height: 23px;
            width: 146px;
        }
        .style5
        {
            height: 23px;
            width: 297px;
        }
        .style1
        {
            height: 23px;
            width: 268px;
        }
        .style7
        {
        }
    </style>
</head>
  
<body>
    <form id="frmSubjects"  runat="server">
    <asp:Panel ID="Panel1" runat="server" BackColor="#FFFF99" BorderColor="#00CC00" 
        BorderStyle="Double" Height="721px">
        <div>
       
        </div>
        <p>
            <asp:TextBox ID="txtIdNumber" runat="server" Visible="False" 
            Width="26px"></asp:TextBox>
        </p>
        <table style="width: 66%; height: 552px;" align="center">
            <tr>
                <td class="style2" colspan="2">
                    Subject Code</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtSubjectCode" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                    <asp:TextBox ID="txtSearch" runat="server" Width="169px"></asp:TextBox>
                                                       
                </td>
            </tr>
            <tr>
                <td class="style3" colspan="2">
                    Subject Name</td>
                <td class="style5" colspan="2">
                    <asp:TextBox ID="txtSubjectName" runat="server" Width="270px"></asp:TextBox>
                </td>
                <td class="style1" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Subject Type</td>
                <td class="style4" colspan="2">
                    <asp:RadioButton ID="rbnCore" runat="server" Text="Core" />
                    <asp:RadioButton ID="rbnElective" runat="server" Text="Elective" />
                </td>
                <td class="style6" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Enter User Name</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Enter Password</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    <asp:Button ID="btnSave" runat="server" style="text-align: center" Text="Save" 
                    Width="93px" />
                </td>
                <td class="style2" colspan="2">
                    <asp:Button ID="btnClear" runat="server" Text="Refresh" Width="92px" />
                </td>
                <td class="style2" colspan="2">
                    <asp:Button ID="btnUpdate" runat="server" Text="Update" Width="96px" />
                </td>
                <td class="style2">
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="97px" />
                </td>
            </tr>
            <tr>
                <td class="style7">
                     </td>
                <td class="style2" colspan="2">
                     </td>
                <td class="style2" colspan="2">
                     </td>
                <td class="style2">
                     </td>
            </tr>
            <tr>
                <td class="style7" colspan="6">
                    <asp:Label ID="lblMessage" runat="server" 
                    style="text-align: center" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style7" colspan="6">
                    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
                      </asp:ScriptManager>
                        <cc1:AutoCompleteExtender ServiceMethod="SearchSubjects"
                        ID="AutoCompleteExtender1"
                        MinimumPrefixLength="1"
                        CompletionInterval="100" EnableCaching="False" CompletionSetCount="10" 
                        TargetControlID="txtSearch"
                         runat="server">
                        </cc1:AutoCompleteExtender>
                </td>
            </tr>
            <tr>
                <td class="style7" colspan="6">
                   
                    <asp:Panel ID="Panel2" runat="server" Height="352px" Width="615px" 
                        ScrollBars="Both">
                        
                        <asp:GridView ID="GridView1" runat="server" CellPadding="2" ForeColor="#333333" 
                            GridLines="None" style="text-align: left; top: 1px; left: 180px;" Width="600px">
                            <RowStyle BackColor="#E3EAEB" />
                            <EmptyDataRowStyle BorderStyle="Solid" />
                            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#7C6F57" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
                        </asp:Panel>
                </td>
            </tr>
        </table>
    </asp:Panel>
        <div>
     </div>
        </form>
</body>
</html>




In the web.config, I added the following:



HTML
<controls>
         <add tagPrefix="cc1" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>
				<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
				<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			</controls>





VB
<System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()> _
  Public Shared Function SearchSubjects(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim conn As SqlConnection = New SqlConnection
        conn.ConnectionString = ConfigurationManager _
             .ConnectionStrings("Data Source=EBENEZERAKAGLO\SQLEXPRESS;Initial Catalog=Naass;Integrated Security=True").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandText = "select SubjectName from ProgramDetails.Subjects where" & _
            " SubjectName like @SearchText + '%'"
        cmd.Parameters.AddWithValue("@SearchText", prefixText)
        cmd.Connection = conn
        conn.Open()
        Dim Subjects As List(Of String) = New List(Of String)
        Dim sdr As SqlDataReader = cmd.ExecuteReader
        While sdr.Read
            Subjects.Add(sdr("SubjectName").ToString)
        End While
        conn.Close()
        Return Subjects
    End Function
Posted

1 solution

According the the ajax library example for autocomplete (found here), your AutoComplete Page method should look something like this:

VB
<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()>
Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()

   Return Nothing
End Function


Your signature looks wrong to me. Your missing the "ByVal contextKey As String" parameter, and your returning a generic list instead of an array of string. The return value may not matter... but it might. The signature difference is probably a problem.

I also noticed that your ToolKitScriptManager and your asp:TextBox do not occur in in the same order on your page as they do in the example. This may also be an issue - if I were having a problem getting this to work, I would stay as close to the published example as possible. In the example, they occur in the page like this:

ASP.NET
   <asp:toolkitscriptmanager id="ToolkitScriptManager1" runat="server" xmlns:asp="#unknown">
</asp:toolkitscriptmanager>

<asp:textbox id="txtMovie" runat="server" xmlns:asp="#unknown"></asp:textbox>


Hope this helps,
- Pete
 
Share this answer
 
v2
Comments
Akaglo 15-May-13 9:42am    
Thanks a lot, but your suggestion is still not working for me. The AutoComplete TextBox is supposed to retrieve the data from a database.
pdoxtader 15-May-13 10:42am    
Just for testing purposes, remove your SQL code form the equation. REM it out, and return a static list. If that works, you know the problem is your SQL code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900