50% OFF!!!

Booking.com

Tuesday, October 7, 2008

AutoCompleteExtender | Run webservice on server side using Reflection



I use this code for running AutoCompleteExtender webservice.
I need to execute on ServerSide for finding the context-key of the selected value.
THis assumes the WebService code include in your project App_Code.

The Web-Service:


[WebMethod]
public string[] GetList(string prefixText, int count)
{
List items = new List();

items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("a","a1"));
items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("b","b1"));
items.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem("c","c1"));

return items.ToArray();
}



The AutoCompleteExtender on page:


< AjaxControlToolkit:AutoCompleteExtender
runat="server"
ID="AutoCompleteExtender_MAIN"
TargetControlID="txtMAIN"
BehaviorID="behavior_MAIN"
ServicePath="WebService.asmx"
ServiceMethod="GetList"
MinimumPrefixLength="2"
DelimiterCharacters=";,:" >




The code for running the web-service method: (fro finding cotext key!)


object objWs = Activator.CreateInstance("App_Code", AutoCompleteExtender_MAIN.ServicePath.Replace(".asmx", "")).Unwrap();
Type wsType = objWs.GetType();
MethodBase mb = wsType.GetMethod(AutoCompleteExtender_MAIN.ServiceMethod);

string[] results = mb.Invoke(objWs, new object[] { searchValue, 1 }) as string[];
if (results != null && results.Length > 0)
{
string[] splitedText = results[0].Split(',', ':', '{', '}', '"');

// place 5:text, place 11:code
string code= splitedText[11];

// set found code to hidden field & other AutoCompleteExtenders context-keys
hidden_code.Value = code;
AutoCompleteExtender_aaa.ContextKey = code;
AutoCompleteExtender_bbb.ContextKey = code;
}