PDA

View Full Version : Problem using Ajax autocomplete extender in VS2005 (.net 2.0)


Whap
04-18-2008, 01:39 AM
Hey all,

I need create an autocomplete text box that pulls it's data from a MySql database. The website I'm working on is written in VB.net, and uses the Microsoft Ajax Toolkit. I've found some examples for how to do this using a webservice in C# or C. I've tried converting them to VB.net, but have been unsucessful in getting the textbox to trigger my "getwordlist" function when the text is changed in the texbox. I'm sure I'm just missing something simple here, but 2 days into this I've been unable to find it. Here is my code. Any help would be greatly appreciated!

.aspx code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="editprofile.aspx.vb" Inherits="Site_editprofile" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!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>Edit Profile</title>
</head>
<body>

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">

</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode=Conditional>
<ContentTemplate>

<asp:Panel ID="EducationPanel" runat="server" BackColor="Lavender" Height="504px" Style="z-index: 11;
left: 8px; position: absolute; top: 8px" Width="600px">
&nbsp;
<asp:Label ID="Label24" runat="server" Font-Names="Arial" Font-Size="Smaller" Style="z-index: 100;
left: 104px; position: absolute; top: 64px" Text="College/University" Width="112px"></asp:Label>
<asp:Label ID="Label25" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Larger"
Style="z-index: 101; left: 248px; position: absolute; top: 8px" Text="Education"
Width="88px"></asp:Label>
<asp:TextBox ID="tbcollege" runat="server" Style="z-index: 102; left: 224px; position: absolute; top: 64px" Width="272px" OnTextChanged="tbcollege_TextChanged"></asp:TextBox> &nbsp; &nbsp;&nbsp;&nbsp;
<asp:Label ID="Label27" runat="server" Font-Names="Arial" Font-Size="Smaller" Style="z-index: 103;
left: 136px; position: absolute; top: 200px" Text="Hgh School" Width="72px"></asp:Label>
<asp:TextBox ID="tbhighschool" runat="server" AutoPostBack="True" Style="z-index: 104;
left: 224px; position: absolute; top: 192px" Width="272px"></asp:TextBox>
&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Style="z-index: 107; left: 272px; position: absolute;
top: 456px" Text="Save Changes" Width="120px" TabIndex="17" />
<asp:Label ID="Label36" runat="server" Font-Bold="True" Font-Names="arial" ForeColor="Green"
Style="z-index: 109; left: 416px; position: absolute; top: 464px" Text="Changes Saved"
Visible="False" Width="184px"></asp:Label>

<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
MinimumPrefixLength="1"
ServiceMethod="GetWordList"
ServicePath="AutoComplete.asmx"
TargetControlID="tbcollege">
</cc1:AutoCompleteExtender>
</contentTemplate>
</asp:UpdatePanel>

</asp:Panel>

</div>
</form>
</body>
</html>

Web service code

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> Public Class AutoComplete
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function GetWordList(ByVal prefixText As String, ByVal count As Int16) As String()
Dim S(count - 1) As String
Dim I As Integer
For I = 0 To count - 1
S(I) = prefixText & " " & Environment.TickCount
Next
Return S
End Function
End Class

abduraooft
04-18-2008, 10:48 AM
Please read this sticky , http://www.codingforums.com/showthread.php?t=82672

Whap
04-18-2008, 02:06 PM
Please read this sticky , http://www.codingforums.com/showthread.php?t=82672

I read it.. It wasn't much help :)

abduraooft
04-18-2008, 02:58 PM
I read it.. It wasn't much help :)It's to help other who read your post!

Whap
04-18-2008, 06:40 PM
It's to help other who read your post!

Ahhh your right. That looks much better. Thanks for editing my post!

Whap
04-19-2008, 01:18 AM
Ok. Since I've obviously posted a tough one here, does anyone know of a better way to create my own autocomplete text box that pulls it's data from a database in VB/asp.net 2.0? (VS2005).

chump2877
04-19-2008, 03:07 AM
Have a look at this page, if you haven't already: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx

I don;t know VB, only C#...But make sure your web service method's signature is correct...And make sure you have included all the necessary properties on the AJAX control to display it correctly...

Edit: and what's going on here, an event handler for your target textbox control:

OnTextChanged="tbcollege_TextChanged"

Do you need that for the AJAX effect, or is that a server side alternative to AJAX in the event that JS is disabled?

Edit #2: and where in your WebMethod are you accessing a database?

NetCoder1234
04-24-2008, 04:54 PM
I had the same problem on a vb project I'm working on. Add a text changed event handler to you text box and execute a DataBind() on your AutoCompleteExtender Control. That should fix your problem.

e.g.

Protected Sub txtSearch_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
AutoCompleteExtender1.DataBind()
End Sub