4

我需要一个用于 ASP.NET 项目的组合框,因此我决定使用 Ajax Control Toolkit 组合框 ( http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx )。

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

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

这是我的 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") 为 null
[打破此错误]
$find('PlantDropDown').add_propertyChanged(function(sender, e) {\r\n

我在 Javascript 端使用 jQuery,顺便说一句,以防万一。

最后更新:
感谢 crescentfresh 的帮助,我让它工作了,最后我的 .aspx 文件中有这个:

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

这是在我的 javascript 文件中,因为我没有在 .aspx 文件中推送 javascript:

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

2 回答 2

9

我相信您应该绑定到"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 控件 ID的常见警告。

api并不容易,这是肯定的。例如,没有.get_value()一种方法可以很好地代替必须通过嵌入的文本框控件。

编辑

> $find("PlantDropDown") 为空

确保您使用的是正确的 id。请参阅客户端中的 .NET 控件 ID。要获得参考,您可能需要执行以下操作:

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

> 我在 javascript 端使用 jQuery

这没有任何意义。

于 2009-09-22T02:48:00.210 回答
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>
于 2009-11-11T08:41:52.573 回答