Skip to content

Commit 14ac459

Browse files
author
Marin Bratanov
committed
docs(checkboxlist): add info on getting selected items
1 parent 1ea10f8 commit 14ac459

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

api/client/Telerik.Web.UI.ButtonListItem.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ slug: Telerik.Web.UI.ButtonListItem
77

88
# Telerik.Web.UI.ButtonListItem
99

10+
This class represents the individual item of a RadCheckBoxList or RadRadioButtonList. You can use the methods to get the current state of the items, or to set it. Note, however, that changes are not sent to the server or persisted after a postback. The only exception is the set_selected() method that can select or deselect an item.
11+
1012
## Methods
1113

1214
### get_text

controls/checkboxlist/client-side-programming/checkboxlist-object.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ The following table lists the most important members of the client-side RadCheck
2020
|**set_visible**|Sets the visibility of the list.|
2121
|**get_enabled**|Returns if the list is enabled.|
2222
|**set_enabled**|Specifies if the list is enabled.|
23-
|**get_selectedIndex**|Gets the index of the selected checkbox.|
23+
|**get_selectedIndex**|Gets the index of the first selected checkbox.|
2424
|**set_selectedIndex**|Checks the checkbox with the provided index.|
25-
|**get_selectedIndices**|Gets an array of the selected checkboxes by indices.|
26-
|**get_selectedItems**|Gets an array of the selected checkbox items.|
27-
|**get_items**|Returns a collection with the checkboxes.|
25+
|**get_selectedIndices**|Gets an array of the indexes of the selected checkboxes.|
26+
|**get_selectedItems**|Gets an array of the selected [checkbox items]({%slug Telerik.Web.UI.ButtonListItem%}).|
27+
|**get_items**|Returns a collection with the [checkbox objects]({%slug Telerik.Web.UI.ButtonListItem%}). You can use it to get or set their state.|
2828
|**get_toolTip**|Gets the text displayed when the mouse pointer hovers over the list. |
2929
|**set_toolTip**|Sets the text displayed when the mouse pointer hovers over the list. |
3030
|**get_height**|Gets the height of the control.|

controls/checkboxlist/functionality/select-item.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ End Sub
103103
104104
## Get Selected Item Server Side
105105

106-
To get the selected item and selected index you can use the `SelectedItem` and `SelectedIndex` properties of the **RadCheckBoxList** control.
106+
To get all selected items, you can loop over the `Items` collection and check for the item's `Selected` property.
107107

108-
>caption Example 4: Get `SelectedIndex` and `SelectedItem` of the **RadCheckBoxList** from the code behind.
108+
To get the first selected item and first selected index you can use the `SelectedItem` and `SelectedIndex` properties of the **RadCheckBoxList** control.
109+
110+
>caption Example 4: Get the selected items of the **RadCheckBoxList** from the code behind.
109111
110112
````ASP.NET
111113
<telerik:RadCheckBoxList ID="RadCheckBoxList1" runat="server" OnSelectedIndexChanged="RadCheckBoxList1_SelectedIndexChanged">
@@ -120,41 +122,83 @@ To get the selected item and selected index you can use the `SelectedItem` and `
120122
````C#
121123
protected void RadCheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
122124
{
125+
//get all selected items via a loop
126+
foreach (ButtonListItem item in RadCheckBoxList1.Items)
127+
{
128+
if(item.Selected)
129+
{
130+
Response.Write(string.Format("item with text {0} and value {1} is selected<br />", item.Text, item.Value));
131+
}
132+
}
133+
134+
//get the first selected item through exposed properties
123135
int selectedIndex = RadCheckBoxList1.SelectedIndex;
124136
ButtonListItem selectedButtonListItem = RadCheckBoxList1.SelectedItem;
125137
}
126138
````
127139
````VB
128140
Protected Sub RadCheckBoxList1_SelectedIndexChanged(sender As Object, e As EventArgs)
141+
'get all selected items via a loop
142+
For Each item As ButtonListItem In RadCheckBoxList1.Items
143+
If item.Selected Then
144+
Response.Write(String.Format("item with text {0} and value {1} is selected<br />", item.Text, item.Value))
145+
End If
146+
Next
147+
148+
'get the first selected item through exposed properties
129149
Dim selectedIndex As Integer = RadCheckBoxList1.SelectedIndex
130150
Dim selectedButtonListItem As ButtonListItem = RadCheckBoxList1.SelectedItem
131151
End Sub
132152
````
133153

134-
The selected item reference provides all its properties (e.g., `Value`, `Text`, `Selected` and `Enabled`).
154+
The `SelectedItem` reference provides all its properties (e.g., `Value`, `Text`, `Selected` and `Enabled`).
135155

136156

137157
## Select Item Client Side
138158

139159
You can select a particular item of a **RadCheckBoxList** by passing the corresponding index in the `set_selectedIndex()` method of the control.
140160

161+
Another approach is to loop through the items via the `get_items()` method and use the `set_selected()` method each [Telerik.Web.UI.ButtonListItem]({%slug Telerik.Web.UI.ButtonListItem%}) item provides.
162+
141163
>caption Example 6: Select an item on the client side.
142164
143165
````JavaScript
144166
var checkboxlist = $find("<%=RadCheckBoxList1.ClientID%>");
167+
//select item by index
145168
checkboxlist.set_selectedIndex(0);
169+
170+
//select item by another condition via its own methods
171+
var items = checkboxlist.get_items();
172+
for (var i = 0; i < items.length; i++) {
173+
if (items[i].get_text() == "Item 3") {
174+
items[i].set_selected(true);
175+
}
176+
else {
177+
items[i].set_selected(false);
178+
}
179+
}
146180
````
147181

148182

149183
## Get Selected Item Client Side
150184

151-
You can obtain the items, selected item and selected item index of **RadCheckBoxList** through the `get_items()`, `get_selectedItem()`, and `get_selectedIndex()` methods.
185+
You can obtain the selected items, first selected item and first selected item index of **RadCheckBoxList** through the `get_selectedItems()`, `get_selectedItem()`, and `get_selectedIndex()` methods.
186+
187+
The `get_selectedItems()` method returns an array of [Telerik.Web.UI.ButtonListItem]({%slug Telerik.Web.UI.ButtonListItem%}) items.
188+
189+
The `get_selectedIndices()` method provides an array of numbers that correspond to the indexes of the selected items.
152190

153191
>caption Example 7: Reference items, selected item and selected index of the **RadCheckBoxList** through its client-side API.
154192
155193
````JavaScript
156194
var checkboxlist = $find("<%=RadCheckBoxList1.ClientID%>");
157195
var items = checkboxlist.get_items();
196+
//loop over all the items and use their selected state
197+
var selItems = cbl.get_selectedItems();
198+
for (var i = 0; i < selItems.length; i++) {
199+
console.log(String.format("text: {0} with value: {1}", selItems[i].get_text(), selItems[i].get_value()));
200+
}
201+
//get the first selected item
158202
var selectedItem = checkboxlist.get_selectedItem();
159203
var selectedIndex = checkboxlist.get_selectedIndex();
160204
````

0 commit comments

Comments
 (0)