有时我们会在一个页面上使用多个AutoCompleteExtender,而且后面的AutoCompleteExtender可能依赖于前面AutoCompleteExtender的结果,这可能是有人说的联动效果吧。
这种效果的实现方法有多种,我的一种方法如下:

第一步:在AutoCompleteExtender中添加一个属性,此属性用于标识AutoCompleteExtender所依赖的控件。
AutoCompleteExtender自动完成条目所依赖的值就来源于此控件。
[DefaultValue("")]
[ExtenderControlProperty]
[IDReferenceProperty(typeof(WebControl))]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
public string ContextControlID
{
    get { return GetPropertyValue("ContextControlID", ""); }
    set { SetPropertyValue("ContextControlID", value); }
}

举个例子,页面上有两个文本框,一个是选择客户,一个是选择客户的联系人,客户联系人文本框所关联的AutoCompleteExtender生成项要依赖于客户文本框的值。
这里联系人文本框所关联的AutoCompleteExtender的ContextControlID属性可以设置为 客户文本框控件的ID属性(或客户编号文本框控件的ID属性)。

第二步: AutoCompleteBehavior.js文件中初始化ContextControlID属性

在AjaxControlToolkit.AutoCompleteBehavior的构造函数中加:
    this._ContextControlID = null;
    this._contextElement = null;

initialize方法中加:
if(this._ContextControlID)
{
    this._contextElement = $get(this._ContextControlID);
}

再添加两个方法:
get_ContextControlID : function() {
/// <value type="String">
/// The ID of the element to display as a modal popup
/// </value>
return this._ContextControlID;
},
set_ContextControlID : function(value) {
if (this._ContextControlID != value) {
    this._ContextControlID = value;
    this.raisePropertyChanged('ContextControlID');
}
},

第三步:修改_onTimerTick方法

找到var params = { prefixText : this._currentPrefix, count: this._completionSetCount };
在后面加上:
params.context = null;
if(this._contextElement)
{
    if(this._contextElement.nodeName && this._contextElement.nodeName.toLowerCase() === "input" && this._contextElement.type == "hidden")
    {
 params.context = this._contextElement.value;
    }
    else
    {
 var contextName = this._contextElement.previousSibling;
 if(contextName)
 {
      while( contextName.nodeName.toLowerCase() != "input")
      {
  contextName = contextName.previousSibling;
      }
      params.context = contextName.value;
  }
    }
}

其中,第一步是给AutoCompleteExtender添加一个属性:ContextControlID,当触发自动完成列表时,要从这个控件里取值,第二步是常规的操作,第三步是修改异步调用时send到服务器端的参数。

当然你用 params.contextKey也可以。

第四步:完成服务器端的WebService
服务器端的WebService不是有两个参数了,因为我们又加了一个context参数,
[WebMethod]
public string[] GetXXXts(string prefixText, int count, string context)
{
    // 这里 context就是从客户端传过来的值,比如,我们的例子中就是客户编号
}

posted on 2010-07-07 15:22  freedom831215  阅读(259)  评论(0编辑  收藏  举报