I am using an AutoCompleteExtender
from AjaxControlToolkit on a TextBox
.
To put it simply, when I drag and drop the AutoCompleteExtender
tool on to my TextBox and then click on "Add AutoComplete page method", I get the following error:
Cannot create page method "GetCompletionlist because no CodeBehind or CodeFile was found!
After googling the error, I basically made my own web-service called AutoCompelte.asmx. Below is the code for that class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace AutoCompleteTest
{
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://microsoft.com/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
// Create array of movies
string[] movies = { "Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II" };
// Return matching movies
return (from m in movies where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select m).Take(count).ToArray();
}
}
}
Of course, the above is dummy data.... Later on, I shall be getting the data from a database.
And my Default.aspx looks like this:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
From
<asp:TextBox ID="txtFrom" runat="server">
</asp:TextBox>
<asp:AutoCompleteExtender runat="server"
ID="txtFrom_AutoCompleteExtender"
TargetControlID="txtFrom"
ServiceMethod="GetCompletionList"
ServicePath="AutoComplete.asmx"
MinimumPrefixLength="2"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="20"
DelimiterCharacters=";, :"
ShowOnlyCurrentWordInCompletionListItem="true">
</asp:AutoCompleteExtender>
When I run the website....and type in the textbox, nothing happens. No Extender is shown. Even if I type "Star".
What am I missing and why was I getting that error at the beginning?
P.S. I am on my University computers so, I think that error might be due to the type of network i am using. not sure.
Any help is HIGHLY HIGHLY HIGHLY appreciated!!
Thanks.
I got it working. This is how I fixed it:
My project was a "Website Application" in Visual Studio. When I simply made a "Website" project, it all worked perfectly.... I don't know why, it just did.
So if anyone else is having the same problem, try port the code over to a "Website" project rather than a "Website Application" project.
Hope that helps.