嘗試所有這一切,但沒有運氣瞎搞,這裏有一個減少我的代碼版本:
List<ListItem> items = new List<ListItem>();
items.Add(new ListItem("abc", "1");
items.Add(new ListItem("xyz", "1");
foreach (ListItem item in items)
{
ddlCompanies.Items.Add(item);
}
的Javascript上textboxs onkeyup事件:
function filter() {
var pattern = document.getElementById('<%= tbFilterText.ClientID %>').value;
var dropDownList = document.getElementById('<%= ddlCompanies.ClientID %>');
if (pattern != null && dropDownList != null) {
/*
* Check if the dropdown list has been backed up before
*/
if (!dropDownList.bak) {
/*
* Backup the current items in the dropdown list
*/
dropDownList.bak = new Array();
for (n = 0; n < dropDownList.length; n++) {
dropDownList.bak[dropDownList.bak.length]
= new Array(dropDownList[n].value, dropDownList[n].text);
}
}
/*
* Loop through the backed up dropdown list and find matches
* for the pattern text.
*/
matches = new Array();
for (n = 0; n < dropDownList.bak.length; n++) {
if (dropDownList.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase()) != -1) {
matches[matches.length] = new Array(dropDownList.bak[n][0], dropDownList.bak[n][1]);
}
}
dropDownList.options.length = 0;
//Add the matched items to the dropDownList
for (n = 0; n < matches.length; n++) {
dropDownList.options[n] = new Option(matches[n][1], matches[n][0]);
}
// If no companies could be found then display a placeholder option
if (dropDownList.options.length == 0) {
dropDownList.options[0] = new Option(document.getElementById('Nothing found').value, "-1");
}
dropDownList.selectedIndex = 0;
}
}
然後點擊一個按鈕這應該導致回發導致錯誤