I have been having trouble with an auto-complete (from database) textbox. It makes an ajax call to a WebMethod in an ASP.NET (VB) webpage, and gives me an "Authentication Error" whenever a letter is typed in the textbox.
The aspx page code is as follows:
<body>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script language="javascript" type="text/javascript">
$(function () {
$('#<%=txtCompanyName.ClientID%>').autocomplete({
source: function (request, response) {
$.ajax({
url: '<%= ResolveUrl("VendorList.aspx/GetCompanyName")%>',
data: "{ 'pre':'" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return { value: item }
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
}
});
});
</script>
<div class="ui-widget" style="text-align:left">
<asp:TextBox ID="txtCompanyName" runat="server" Width="350px" CssClass="textboxAuto" Font-Size="12px" />
</div>
</body>
The VB.NET code behind file code is as follows:
Partial Class VendorList
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod(ResponseFormat:=Script.Services.ResponseFormat.Json)> _
Public Shared Function GetCompanyName(ByVal pre As String) As List(Of String)
Dim allCompanyNames As New List(Of String)
Using myEntities As New VendorEntities()
allCompanyNames = (From vendor In myEntities.Vendors
Where vendor.VendorName.StartsWith(pre)
Select vendor.VendorName).ToList()
End Using
Return allCompanyNames
End Function
End Class
I have also tried to create a Web.config file in the directory that this page is contained in, with the following code:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
I am also using FriendlyURLS.
Any ideas on how to get around this Authentication error? Thanks
Update
I now have also tried the AjaxControlToolkit AutoCompleteExtender, but I am only getting back a list of the HTML markup for the page, with one character per list item.
The code for the aspx page is as follows:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true"></asp:ScriptManager>
<asp:TextBox ID="txtVendorNameSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender
MinimumPrefixLength="2" ServiceMethod="SearchVendorNames"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtVendorNameSearch"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
</asp:Content>
The code for the VB.NET code behind file is as follows:
Partial Class Operations_Inventory_AutoCompleteTest
Inherits BasePage
<System.Web.Script.Services.ScriptMethod(), _
System.Web.Services.WebMethod()> _
Public Function SearchVendorNames(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
Dim vendorNames As List(Of String) = New List(Of String)
Using myEntities As New VendorEntities()
Dim vendors = (From vendor In myEntities.InventoryVendors
Where vendor.VendorName.StartsWith(prefixText)
Select vendor.VendorName, vendor.ID).ToList()
For Each item In vendors
vendorNames.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(item.VendorName, item.ID.ToString))
Next
End Using
Return vendorNames
End Function
End Class