3

我需要ASP.NET项目的组合框,因此我决定使用Ajax Control Toolkit组合框(http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx)。当文本框发生变化时,如何将JavaScript事件处理程序绑定到Ajax Control Toolkit组合框

我不想使用回发,因为我不希望重新加载页面,但我需要知道文本框中的文本何时发生更改,以便我可以调用服务器来保留新的列表项。

我很好奇我如何将onchange或onblur事件绑定到此组合框使用的输入框。

这是我的asp.net页面:

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %> 

<cc1:ComboBox ID="PlantDropDown" runat="server" OnInit="PlantDropDown_Init" DropDownStyle="DropDown" 
      AutoCompleteMode="SuggestAppend" 
      ItemInsertLocation="OrdinalText" AutoPostBack="false"> 


       </cc1:ComboBox> 

更新:我尝试使用建议和我得到这个错误:

$find("PlantDropDown") is null 
[Break on this error] $find('PlantDropDown').add_propertyChanged(function(sender, e) {\r\n 

我使用jQuery的JavaScript端,顺便说一句,如果有帮助。

最后更新: 我得到它的工作多亏了crescentfresh帮助,并在最后我有这在我的.aspx文件:

<input type="hidden" id="PlantDropDownID" value="<%= PlantDropDown.ClientID %>" /> 

这是我的JavaScript文件,因为我不“T推的javascript在我的.aspx文件:

elem = document.getElementById('PlantDropDownID'); 
$find(elem.value).add_propertyChanged(function(sender, e) { 
    if (e.get_propertyName() == 'selectedIndex') { 
     var newValue = sender.get_textBoxControl().value; 
    } 
}) 

回答

8

我相信你应该绑定到"propertyChanged"事件,并检查是否有更改"selectedIndex"属性:

$find('PlantDropDown').add_propertyChanged(function(sender, e) { 
    if (e.get_propertyName() == 'selectedIndex') { 
     var newValue = sender.get_textBoxControl().value; 

     // persist selected value here... 
    } 
}) 

与通常的告诫约.NET control ids in the client

api不容易,这是肯定的。例如,没有.get_value()方法,这将是很好的,而不必经过嵌入式文本框控件。

编辑

> $找到( “PlantDropDown”)为空

确保您使用的是正确的ID。见.NET control ids in the client。为了得到一个参考,你可能需要做:

$find('<%= PlantDropDown.ClientID %>') 

>我使用jQuery的JavaScript端

持有无关。

+0

谢谢,我正在测试它,希望我能早日知道它是如何工作的。 – 2009-09-22 03:36:07

+0

不幸的是我收到了一个错误,在我的问题中提出了更新。 – 2009-09-22 13:02:56

+0

我在回答中添加了更新。 – 2009-09-22 14:06:23

1

我发现我无法获得提供的答案,除非我将它封装在如下所示的函数中。不知道这是为什么,但希望它可以节省别人的一些痛苦。

<script language="javascript" type="text/javascript"> 

    Sys.Application.add_load(initializePage); 

    function initializePage() { 
     $find('PlantDropDown').add_propertyChanged(function(sender, e) {  
     if (e.get_propertyName() == 'selectedIndex') {   
     var newValue = sender.get_textBoxControl().value;   
     // persist selected value here...  
     }}) 
    } 

</script> 
+1

在控件初始化之前,您不能与控件进行交互(例如,除非控件实际存在,否则不能在控件上调用add_propertyChanged)。控件在ASP.NET AJAX框架的初始化阶段完成之前不存在。所以你所做的完全正确。完整的内幕请参阅http://www.asp.net/ajax/documentation/live/overview/AJAXClientEvents.aspx。 – 2009-11-15 04:08:50

+0

顺便说一句,如果你想获得selectedIndex值而不是所选项目的文本,请使用'sender.get_hiddenFieldControl().value'而不是'sender.get_textBoxControl().value'。 AJAX Combobox使用文本框,无序列表和隐藏字段将所有控件集中在一起。 – atconway 2012-03-20 14:35:21

相关问题