<telerik:RadListBox ID="AttachmentsRadListBox" CheckBoxes ="true" runat="server" />
it is located in a RadWindow, therefore I am populating it through the following code which is only called when RadWidnow becomes visible:
AttachmentsRadListBox.DataSource = AttachDT
AttachmentsRadListBox.DataTextField = "DocumentPath"
AttachmentsRadListBox.DataValueField = "DocumentID"
For Each item As RadListBoxItem In AttachmentsRadListBox.Items
item.Checked = True
So far so good, the RadListBox is populated and all the items are checked.
Now, there is a Save button on the RadWindow when pressed before closing the window I am trying to read the checked items in the AttachmentsRadListBox (Since the user might have changed the status of the checked items). But every effort on reading the items has failed, for example on the Save button click I have the following:
Dim test As Integer = AttachmentsRadListBox.Items.Count // THIS IS ZERO
For Each item As RadListBoxItem In AttachmentsRadListBox.Items // THERE ARE NO ITEMS
If Not item.Checked Then
Dim DocumentIDToDelete As Integer = item.Value
Why is that the last piece of code does not behave as I hope? The AttachmentsRadListBox is not being bounded again through the postback. The only time that it is bounded is when the RadWindow appears. Then the Save button on the RadWindow obviously creates a postback but I don't understand why AttachmentsRadListBox contains no item at that point.
7 Answers, 1 is accepted
Please have a look into the following code I tried which works fine at my end.
Thanks for getting back to me. There is a difference between the way I am binding the RadListBox to the data and the way you are. I am querying the SQL database in the code behind and creating the Datatable (AttachDT) in the Code behind and then setting AttachmentsRadListBox.DataSource = AttachDT. You, on the other hand, have defined a SQLDataSource in .apsx. Even though this difference should not make a difference in accessing the RadListBox items but I have reasons to believe that it is the case here.
I cannot take your approach, I have to create the data table in the code behind. Could you please verify that with my approach (as far as the binding) I can get what I am looking for as I asked in my original question. Thanks!
Once the RadListBox is correctly populated with data, you should have access to the CheckedItemsCollection. Would you provide us more detailed information on the configuration that you are using at your end, so we could try to replicate the faced issue locally?
But then in the method that handles Save Button Press, the following:
I don't have access to the RadListBox items.
Since the RadWindow is closing upon pressing the save button, I do not need to recreate the RadListBox after postback to display it again but what I do need to do is to be able to read the checked status of the checkboxes, but since that is after a post back, I already lost that. So how do I preserve the check status of the checkboxes right before pressing Save?
Please check the following code snippet I tried which works fine as expected.
ONLY DataTextField is being set in Shinu and Princy's examples - then they are reading the item.Value (which I guess defaults to the Item.Text if the DataValueField is not defined.
What original question had was DIFFERENT DataValueField and DataTextField definitions. Which is exactly the way that I (and a lot of other people) do it. But when you read the stuff from the ListBox, the item.Text and item.Value contents are from the DataTextField column and NOT the DataValueField column. I can read the item.DataKey value - but that value often mysteriously (and frustratingly!) becomes null.
All that was being asked is that why doesn't the DataValueField and DataTextField work the same as they do in the RadComboBox?