简体   繁体   English

如何使用脚本控件(ASP.Net)获取“自动完成控件”脚本端

[英]How do I Get the Autocomplete Control script side with a Script control (ASP.Net)

Problem How do I capture and assign the events on an Ajax Toolkit autocomplete control using a script control on the script file? 问题如何使用脚本文件上的脚本控件在Ajax Toolkit自动完成控件上捕获并分配事件?

Explanation 说明

I basically created a script control to combine a textbox and an autocomplete control so that I could have a working generic control for an autocomplete. 我基本上创建了一个脚本控件,将文本框和自动完成控件结合在一起,以便可以为自动完成提供有效的通用控件。 The next step was to add things like a processing image while it searches for its items. 下一步是在搜索项目时添加诸如处理图像之类的东西。 It seemed easy enough. 这似乎很容易。

protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{  
  ScriptControlDescriptor desc = new ScriptControlDescriptor   
     ("NDI.WebControls.Client.GenericAutoComplete", this.ClientID);
  desc.AddProperty("autoCompleteID", autoComplete.ClientID);

  return new ScriptDescriptor[] { desc };
}

And then on the javascript the normal: 然后在javascript上正常:

initialize: function()
{
  this._autoComplete = $get(this._autoCompleteID);  
  //this._autoCompleteID does have a value

  this._autoCompleteClientPopulating = 
     Function.createDelegate(this, this.handleAutoCompleteClientPopulating);

  $addHandler(this._autoComplete, "clientPopulating", 
     this._autoCompleteClientPopulating);

  NDI.WebControls.Client.GenericAutoComplete.callBaseMethod(this, 'initialize');
},

Now this should work BUT it doesn't. 现在这应该可以,但不能。 Why? 为什么? Because apparently there is no autocomplete control rendered to the page like a normal control would be. 因为显然没有像普通控件那样呈现给页面的自动完成控件。 So when it gets to the $get part it comes up null despite the ID property having a text property. 因此,当到达$ get部分时,尽管ID属性具有text属性,但它变为null。 (IE the control doesn't exist) (即该控件不存在)

Is this possible to do or do I have to use the OnXyz properties server side to assign a method? 是否可以这样做,或者我必须使用OnXyz属性服务器端来分配方法? As in: 如:

  autocomplete.OnClientPoplating = someScript;

ANSWER 回答

Booyah Found it. Booyah找到了。 Turns out the autocomplete has a built in way to access it's events in javascript: 事实证明,自动完成功能具有内置的访问javascript中事件的方式:

Server side you have to set the BehaviorID: 在服务器端,您必须设置BehaviorID:

autoComplete.BehaviorID = "autoCompleteBehavior";

And then set it in the GetScriptDescriptors method: 然后在GetScriptDescriptors方法中进行设置:

protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
  ScriptControlDescriptor desc = new 
   ScriptControlDescriptor("NDI.WebControls.Client.GenericAutoComplete", ClientID);
   desc.AddProperty("autoCompleteID", autoComplete.BehaviorID);

  return new ScriptDescriptor[] { desc };
}

Of course you have to add the properties script side to capture that value, and once you do that you have to use Find to get it. 当然,您必须添加属性脚本来捕获该值,一旦执行该操作,就必须使用“查找”来获取它。 Then you have to create the event handler: 然后,您必须创建事件处理程序:

this._autoComplete = $find(this._autoCompleteID);
this._onAutoCompletePopulating = 
   Function.createDelegate(this, this.handleOnAutoCompletePopulating);

Finally use the built in event setter in the autocomplete control (Behavior object): 最后,使用自动完成控件(Behavior对象)中的内置事件设置器:

this._autoComplete.add_populating(this._onAutoCompletePopulating);

And boom, it's set. 和繁荣,它已经设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM