The MS Ajax AutoComplete extender is able to pull its completion list from either a Web Service or a Page Method. All the examples I could find used Web Service, but I wanted to use the Page Method approach.
As I understand it (not very well) the advantages of a page method are that
- The code for the completion list callback is kept with the rest of the code for the page containing the control.
- I can easily access session state in the callback method.
To get the extender to work with page methods requires several modifications to the page.
First, we need to tell the AJAX script manager to allow callbacks to page methods.
<asp:ScriptManager
ID=”scriptManager”
EnablePageMethods=”true”
runat=”server” >
Second, we need to set up the AutoComplete extender to call a page method. The main thing here is that there is no ServicePath property.
<ajaxtoolkit:AutoCompleteExtender
runat=”server”
ID=”autoComplete1″
TargetControlID=”TextBox1″
ServiceMethod=”GetCompletionList”
MinimumPrefixLength=”2″
CompletionInterval=”1000″
EnableCaching=”true”
CompletionSetCount=”12″ />
Finally, we implement the method that returns the completion list in the code-behind.
<WebMethod()> _
<Script.Services.ScriptMethod()> _
Public Shared Function GetCompletionList( _
ByVal prefixText As String, _
ByVal count As Integer) _
As String()
‘ Insert code to return a string array here…
End Function
Notice the signature of this method. It must be shared (static in c#) method. The parameters passed to the method must be called “prefixText” and “count”. Nothing else will work. However, you are permitted to return a List(Of String), instead of an array of strings, if that suits you better.
Of course, you’ll need to import “System.Web.Services” at the top of the code-behind, so you will need to add a reference to “System.Web.Services” to your project if you don’t already have one.
My thanks to Fredrik Normén for his blog entry on Calling a static page method with ASP.Net Ajax.
March 27, 2007 at 7:26 am |
To bad it has to be a static page method, hope this will be solved with the next Ajax release…
August 28, 2007 at 3:55 pm |
Thanks for this entry. I was having the exact same issue. Your solution worked with my application.
August 28, 2007 at 7:31 pm |
Seth:
I’m glad this was useful to you. Your feedback is very much appreciated.
November 5, 2007 at 8:31 pm |
Thanx for this guide. I did not want webservice, since I could not get my session based connection strings accessed. Now I get to play with styling this. I used just what you did above and it worked great ! Great job! Tom
November 6, 2007 at 12:32 pm |
Tom:
Thanks for the feedback. It is good to know that I have written something useful. All the best with the styling.
November 13, 2007 at 5:17 pm |
Thank you for saving my company from buying me another keyboard. I wore the last one out banging my head against this problem.
November 14, 2007 at 11:58 am |
neontapir:
I quite understand your frustration. I came close to throwing my PC out the window figuring out the solution. I’m pleased that you found it useful.
November 23, 2007 at 10:50 pm |
[...] http://allwrong.wordpress.com/2007/03/13/ms-ajax-autocomplete-extender-using-a-page-method/ [...]
December 13, 2007 at 7:22 am |
This article helped me keep it up
thanks…
January 19, 2008 at 2:58 pm |
Thank you very much! This was a big help – hopefully MS will make this part of their out-of-the-box solution with the next version.
January 21, 2008 at 1:33 pm |
Ahsan Raza / Chris Fuller:
That’s great!
March 14, 2008 at 2:15 am |
Great !!…But…I failed…
No response for my application….is any missing ?
My Autocomplete Extender and Textbox are placed inside the UpdatePanel….
is it work??
Do you have complete example for me?? Many thanks..
March 14, 2008 at 2:13 pm |
Gio:
I am sorry, the work I did on this is owned by the company I work for, so I can’t publish it.
But, don’t give up! I’m sure you can figure it out.
March 18, 2008 at 9:16 am |
Thanks alot it hepl me alot……
March 18, 2008 at 9:06 pm |
Simi:
Thanks for the kind words. I’m pleased I could help.
March 26, 2008 at 12:58 am |
You rock most righteously.
Many thanks.
July 8, 2008 at 2:05 pm |
Hi. i have the textbox and autocompleteextender on a usercontrol but doesnt work if i dont have the method on the aspx… it seems that it doesnt find it on the usercontrol codebehind…
some help please!!!
July 15, 2008 at 9:03 pm |
Bernardo:
I am sorry, I have never tried this configuration.
July 17, 2008 at 1:20 am |
Gio –
I did spend a lot of time in making this work – just wanted to list out the following things i noticed
1)My text box is placed inside a update panel which is placed inside a collapsible panel
2)Placing breakpoints in the code behind method ALWAYS screwed up the result
3)using System.Web.Services;
using System.Collections.Generic;
using System.Web.Script.Services;
these three need to be present in the code behind where the method is being implemented
4)Also do note that any variables/data structures used in the static method would retain data and might potentially cause errors -for instance an array used for retrieving dtaa from a DB
Hope this helps – Happy programming – and thanks al ot K for this wonderful post
July 21, 2008 at 2:37 pm |
It doesn´t work in a WebPart deployed on Sharepoint 2007
August 13, 2008 at 9:24 am |
10x this is what i have been looking for!!! worked gr8
October 14, 2008 at 9:04 am |
Занимаюсь дизайном и хочу попросить автора allwrong.wordpress.com отправить шаьлончик на мой мыил) Готов заплатить…
December 13, 2008 at 5:53 pm |
Thanks!,
December 15, 2008 at 8:39 am |
Yes, I have tried that configuration too. The AutoCompleteExtender control doesn’t work well inside a web user control. I think it will always look for the web service method inside the page containing the user control. Ouch! That hurts.
January 20, 2009 at 4:57 pm |
Hi,
this is really great but i coudn’t translate this to c# code.
can you help me please??
January 22, 2009 at 6:02 pm |
Solicitud
// Work around browser behavior of “auto-submitting” simple forms
var frm = document.getElementById(“aspnetForm”);
if (frm) {
frm.onsubmit = function() { return false; };
}
——————-
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Web.Services;
namespace pepe
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string[] GetCompletionList(string prefixText, int count)
{
if (count == 0)
{
count = 10;
}
if (prefixText.Equals(“xyz”))
{
return new string[0];
}
Random random = new Random();
List items = new List(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65, 90);
char c2 = (char)random.Next(97, 122);
char c3 = (char)random.Next(97, 122);
items.Add(prefixText + c1 + c2 + c3);
}
return items.ToArray();
}
}
}
January 22, 2009 at 6:08 pm |
Solicitud
// Work around browser behavior of “auto-submitting” simple forms
var frm = document.getElementById(“aspnetForm”);
if (frm) {
frm.onsubmit = function() { return false; };
}
February 20, 2009 at 11:29 pm |
Kramii, Thanks so much!! I don’t know why this is not documented elsewhere. Life saver indeed.
March 31, 2009 at 8:21 pm |
Very nice! Works like a charm. I always enjoy simple and concise examples.
May 27, 2009 at 11:10 am |
I love this, many of my issues have been resolved thanks for the great help. God bless you
June 8, 2009 at 7:03 pm |
You’re the man, the most consise explanation on the web right now! Thanks!
June 10, 2009 at 10:08 am |
Excellent article. I have been lost 6 hrs yesterday trying to make this work and as I came in this morning, found the page and lo! in few minutes I was able to setup. Thanks a ton for posting this.
August 4, 2009 at 1:07 am |
Thank you! I had spent all morning trying to configure Page Method for a User Control. Turns out all I needed to do was put the method on the calling Web Page’s code-behind instead of the UC’s.
Not as self-contained as I’d like it to be, but it works.
September 3, 2009 at 10:10 am |
Very Very Very Thanks for ur great code , its working perfectly.
September 16, 2009 at 6:39 pm |
It’s working great! Thanks for sharing!
October 22, 2009 at 10:40 am |
While the process is running, a portion of the context, called the working set, is resident in memory. ,