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

相關問題