Hello,
I have two RadListBox controls. One holds selected items, the other holds the source list of items. Users can select items by transferring them from the source to selected RadListBox. The source list box can be filtered on the fly and I have implemented a web service that gets a new source list. The following JavaScript function is called upon successful completion of the web service call. The data parameter is of type RadComboBoxData which holds the needed return data nicely. There is no equivalent RadListBoxData that I could find.
The issue I'm having is sometimes this function gets an error "f.getItem(this) is undefined". This does not happen that often but often enough. I'm still trying to figure out how to reliably reproduce the error. What I can say for sure is that sometimes when the below function is called to refresh the source list box this error happens and then nothing can be transferred between the two RadListBox controls.
function fillMultiSelect(data) {
var listBoxSelected = $find('<listBoxSelected ID>');
var itemsSelected = listBoxSelected.get_items();
var listBox = $find('<listBoxSource ID>');
var itemsSource = listBox.get_items();
itemsSource.clear();
for (var i = 0; i < data.NumberOfItems; i++) {
if (!data.Items[i]) break;
var item = new Telerik.Web.UI.RadListBoxItem();
item.set_text(data.Items[i].Text);
item.set_value(data.Items[i].Value);
var found = false; // don't add items already selected
itemsSelected.forEach(function(itemS) { if (itemS.get_text() == item.get_text()) found = true; });
if (!found) itemsSource.add(item);
}
}
Your help would be appreciated.
16 Answers, 1 is accepted
I am not sure why this error occurs. However you can use the findItemByText method instead of manually traversing the items collection.
Regards,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
findItemByText should be a method of the RadListBox object. I noticed that this method is not yet public, so you can use the private one: _findItemByText.
We will add the public method in the next service pack.
All the best,
Veselin Vasilev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
I have a very similar scenario, in which a RadListBox item that is created in client code and added to the item collection. Before doing this, I need to check whether an item with the same value (or text) already exists in the collection. I've tried a few alternatives to search the collection using the private methods of the listbox object, but none worked for me.
Finally, I've tried your solution, but still with no luck: while using listbox._findItemByText(text) - I've received the error "'undefined' is null or not an object". Both the listbox and the text variables are defined and are seen by the debugger, the error occurs in Telerik code:
_getAllItemsRecursive:function(d,a){var b=a._getChildren();
for(var e=0;e<b.get_count();e++){
var c=b.getItem(e);
Array.add(d,c);
this._getAllItemsRecursive(d,c)
}}
The error occurs while calling the b.get_count() - b is undefined.
Hoewever, this does not occur everytime. Do you have any other solution or workaround to this problem?
Thank you,
Adrian
_findItemByText should always work. Perhaps you have discovered a bug in RadListBox. Could you please provide additional info when this error occurs? Ideally you could paste here your code or send us a sample page in a support ticket.
Regards,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
More details about the scenario:
- the code is used in a web user control, on the page multiple instances are present
- in the left part, a listbox is present, in the right part a treeview
- when clicking on a treenode, the node is hidden and if the listbox doesn't contain another item with the same text as the treenode, a new item is created. It's done by calling the client function below.
The code:
dualTreeSelector.prototype.MoveNodeToList =function(node)
{
var listbox = $find(this.listBox); //this.listBox is the id of the listbox
var treeview = $find(this.treeView); //this.treeView is the id of the tree
//construct a new list item
var item = new Telerik.Web.UI.RadListBoxItem();
item.set_text(node.get_text());
item.set_value(node.get_value());
if (listbox._findItemByText(node.get_text()) ==null) //this is where it fails
{
//add the new list item
listbox.trackChanges();
listbox.get_items().add(item);
listbox.commitChanges();
}
//hide the tree node
node.set_visible(false);
}
Unfortunately this code does not indicate the reason for the problem. Is it possible to send us a live url or attach a runnable version of your page? I am afraid this is the only way to debug this issue.
Regards,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Test2.aspx.vb" Inherits=".Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function MoveNodeToList(node)
{
var listbox = $find('RadListBox1');
var treeview = $find('RadTreeView1');
//construct a new list item
var item = new Telerik.Web.UI.RadListBoxItem();
item.set_text(node.get_text());
item.set_value(node.get_value());
debugger;
if (listbox._findItemByText(node.get_text()) ==null) //this is where it fails
{
//add the new list item
listbox.trackChanges();
listbox.get_items().add(item);
listbox.commitChanges();
}
//hide the tree node
node.set_visible(false);
}
</script>
<div>
<asp:ScriptManager runat="server" id="scriptManager1"></asp:ScriptManager>
<radC:RadListBox ID="RadListBox1" runat="server">
<Items>
<radC:RadListBoxItem Text="Existing item" Value="0" />
</Items>
</radC:RadListBox>
<radC:RadTreeView ID="RadTreeView1" runat="server" OnClientNodeClicked="function(sender,eventArgs){MoveNodeToList(eventArgs.get_node());}">
<Nodes>
<radC:RadTreeNode Text="Existing item" Value="0"></radC:RadTreeNode>
<radC:RadTreeNode Text="Another item" Value="1"></radC:RadTreeNode>
</Nodes>
</radC:RadTreeView>
</div>
</form>
</body>
</html>
We have added the fore mentioned methods to the RadListBox. You can test them with the latest internal build. If you want to try them immediately place the following code right after the ScriptManager declaration:
<script type="text/javascript"> |
Telerik.Web.UI.RadListBox.prototype.findItemByText = function(theText) { |
var items = this.get_items(); |
var itemsCount = items.get_count(); |
for (var i = 0; i < itemsCount; i++) { |
if (items.getItem(i).get_text() == theText) { |
return items.getItem(i); |
} |
} |
return null; |
} |
Telerik.Web.UI.RadListBox.prototype.findItemByValue = function(theValue) { |
if (!theValue) return null; |
var items = this.get_items(); |
var itemsCount = items.get_count(); |
for (var i = 0; i < itemsCount; i++) { |
if (items.getItem(i).get_value() == theValue) { |
return items.getItem(i); |
} |
} |
return null; |
} |
</script> |
Then you can use the methods like this:
<script type="text/javascript"> |
function pageLoad() { |
var listBox = $find("RadListBox1"); |
var item = listBox.findItemByText("RadListBoxItem7"); |
} |
</script> |
Sincerely yours,
Genady Sergeev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tried today using both methods (findItemByValue() & findItemByText()) but kept getting similar error: Object doesn't support this property or method. However, implementing the Prototype declaration as suggested by Genady appears to sort that out.
I'm on build 723. Checked released notes of subsequent builds but found no mention to inclusion of said methods. Any chance of having them included soon?
Thanks.
Best regards,
Saed
The findItemByText, findItemByValue and findItemByAttribute methods are already included in the latest service pack. The service pack version is 826. Perhaps something has gone wrong with the release notes, I will check this out.
Kind regards,
Genady Sergeev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Like others I am trying to access the attributes from javascript. I am successfully adding the attr via ItemDataBound..
Protected Sub RadListBox3_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadListBoxItemEventArgs) Handles RadListBox3.ItemDataBound
Dim dataSourceRow As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
e.Item.Attributes(
"UsergroupID") = dataSourceRow("UsergroupID").ToString()
End Sub
I have testing and confirmed the attribute is serialized in HTML runtime.
The javascript is halting with the execution of the findItemByAttribute() method.
var
item = listbox3.findItemByAttribute("UsergroupID", 1);
The processdies on line 1257 of the Telerik axd file, at the line..
_getAllItemsRecursive:function(d,a){var b=a._getChildren();
for
(var e=0;
e<b.get_count();
e++){
var c=b.getItem(e);
Array.add(d,c);
this
._getAllItemsRecursive(d,c)
}}
b is null.
I tried your code and I am also unable to search the item using attribute value which is set through ItemDataBound event. As a workaround I tried following code to get the corresponding item without using findItemByAttribute() method.
JavaScript:
<script type="text/javascript"> |
function SerchGetAttribute() { |
var listBox = $find('<%= RadListBox1.ClientID %>'); |
var item; |
for (var i = 0; i < listBox.get_items().get_count(); i++) { |
if (listBox.get_items().getItem(i).get_attributes().getAttribute("UsergroupID") == "1") { |
item = listBox.get_items().getItem(i); |
} |
} |
alert(item.get_text()); |
} |
</script> |
-Shinu.