Wednesday, March 21, 2012

Is AutoCompletes ContextKey property useless....?

.....or is it just me? I tried using this property, and yes, the string does get passed to the web service method. However, when I check HttpContext.Current.Items, any objects which had previously been added to the Items collection no longer exist. Even HttpContext.Current.Session is null. This makes sense because, with each user keypress, a new HttpContext is created to invoke the web method.

There seem to be no events that can be handled before the web method is invoked. So my question is, exactly what functionality, aside from allowing a user to pass an additional string paramter to the web method, does this property provide? If there's a configuration setting I need to change, or if I'm going about this in totally the wrong way, someone please tell me! I'm using build 10618 of the toolkit.

Thanks,

Dan

Hi Danludwig,

Sometimes we want to send back more parameters instead of the TextBox's value only. For example, if one WebMethod services serverl AutoCompleteExtenders, we need more parameters to Distinguish different Controls or useages. We can past more parameters by using ContextKey. For example: "parameter1, parameter2,parameter3...";

To set its ContextKey, we can use $find("AutoCompleteExtenders.BehaviorID").set_contextKey("your parameters"); Here is the sample.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <style> .AutoExtender { font-family: Verdana, Helvetica, sans-serif; font-size: .8em; font-weight: normal; border:solid 1px #006699; line-height:20px; padding:2px; background-color:White; } .AutoExtenderList { border-bottom:dotted 1px #006699; cursor:pointer; color:Maroon } .AutoExtenderHighlight { color:White; background-color:#006699; cursor:pointer; } </style></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TextBox ID="TBSearch" runat="server"></asp:TextBox> <asp:Panel runat="server" ID="myPanel" Height="100px" ScrollBars="Vertical"> </asp:Panel> <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" BehaviorID="myACEBID" TargetControlID="TBSearch" ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionListCssClass="AutoExtender" CompletionListItemCssClass="AutoExtenderList"
 CompletionListHighlightedItemCssClass="AutoExtenderHighlight" CompletionInterval="1" EnableCaching="true" CompletionSetCount="12" CompletionListElementID="myPanel"UseContextKey="true" ContextKey="Jonathan"/> <br/><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input id="Button1" type="button" value="button" onclick="changeContextKey()"/> <script type="text/javascript" language="javascript"> function changeContextKey(){ $find("myACEBID").set_contextKey('tommy'); } </script> </form></body></html>

<%@. WebService Language="C#" Class="AutoComplete" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

public AutoComplete()
{
}

[WebMethod]
public string[] GetCompletionList(string prefixText, int count, string contextKey)
{
//do something on contextKey to get the parameters.
if (count == 0)
{
count = 10;
}

if (prefixText.Equals("xyz"))
{
return new string[0];
}

Random random = new Random();
List<string> items = new List<string>(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();
}

}

I hope this help.

Best regards,

Jonathan

No comments:

Post a Comment