Reputation: 3710
I can't get AJAX CT Autocomplete to work. The problem is when I start writing in textbox nothing happens. The frist problem I had experienced was when I tried to Add AutoComplete page method I got an error: "Cannot create page method "GetCompletionList"...". Then I tried creating it manually, but still nothing happens.
Here is the AdministracijaOsoba.aspx code:
<asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox><asp:AutoCompleteExtender
ID="AutoCompleteExtender1" runat="server" ScriptPath=""
ServiceMethod="GetCompletionList" ServicePath="AdministracijaOsoba.aspx.cs"
TargetControlID="txtOsoba" UseContextKey="True">
</asp:AutoCompleteExtender>
Here is the AdministracijaOsoba.aspx.cs code:
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();
var osoba = from o in db.osobas
orderby o.osoba_prezime
select new { o.osoba_id, person = o.osoba_prezime + " " + o.osoba_ime };
string[] main = new string[0];
foreach (var o in osoba)
{
if (o.person.StartsWith(prefixText))
{
Array.Resize(ref main, main.Length + 1);
main[main.Length - 1] = o.person.ToString();
if (main.Length == 15)
{
break;
}
}
}
Array.Sort(main);
return main;
}
Take a note that I'm using LINQ to Entities. Any help on this would be appreciated.
Regards!
Upvotes: 1
Views: 18799
Reputation: 1
Ajax autocomplete uses a service call, so you can use the below code in your aspx.cs file, Note the System.Web.Services.WebMethodAttribute()
attribute, this will make the method accessible for Service call.
Alternatively you can use any ASMX service or WCF service for extensive and reliable service use.
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetCompletionList(string prefixText, int count, string contextKey)
{
return UserControls_phonenumbersearch.GetCompletionList(prefixText, count, contextKey);
}
Upvotes: -1
Reputation: 15973
Perhaps you are missing specifying MinimumPrefixLength parameter of AutoCompleteExtender.
Upvotes: 0
Reputation: 1710
I too have been having the same problem. I know this is a bit late, but better late than never...
Here is the setup that finally worked for me (with your IDs and names in place):
Code-Behind ( aspx.cs ):
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethod()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
...
}
Code ( .aspx ):
<asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1"
runat="server"
ServiceMethod="GetCompletionList"
TargetControlID="txtOsoba"
UseContextKey="True">
</asp:AutoCompleteExtender>
As you can see, you don't need to set the ScriptPath and ServicePath properties because these properties represent...
The path to the web service that the extender will pull the word\sentence completions from. If this is not provided, the service method should be a page method.
which is explained in the AutoComplete Reference Page. You've got your GetCompletionList() method defined in your Code-Behind, which I am currently assuming qualifies as "a page method". So, it seems you would only use the Path properties if we had the method in a different location such as a services.cs or something of that sort.
Upvotes: 1
Reputation: 1696
Your code behind should read like this
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetCompletionList prefixText, int count, string contextKey)
{....}
Also, there is no need of providing the servicepath attribute for your ajax extender if you are using a pagescriptmethod.
Upvotes: 2
Reputation: 585
Your code is almost right. The only problem is that the service path shouldnt end with .aspx.cs but only .aspx. If the extender is on the same page as the method then leave out servicepath
Upvotes: 1
Reputation: 11
Here's what I have in my aspx page:
<asp:AutoCompleteExtender ID="tbSearchName_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServicePath=""
TargetControlID="tbSearchName" ServiceMethod="GetCompletionList"
UseContextKey="True" MinimumPrefixLength="2">
</asp:AutoCompleteExtender>
In my code behind page I have:
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
ArrayList testList = new ArrayList();
...
return (string[])testList.ToArray(typeof(string));
}
Upvotes: 1
Reputation: 21117
Change your declaration to this:
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
ServiceMethod="GetCompletionList"
ServicePath="AdministracijaOsoba.aspx/GetCompletionList"
TargetControlID="txtOsoba" UseContextKey="True">
Add this to your AdministracijaOsoba.aspx.cs code:
[WebMethod]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
...
}
Upvotes: 1