Accessing RadListBox Items in Code-Behind

2.5k Views Asked by At

I have the following RadListBox:

   <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"
AttachmentsRadListBox.DataBind()
For Each item As RadListBoxItem In AttachmentsRadListBox.Items
     item.Checked = True
Next

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
   End If
Next

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.

1

There are 1 best solutions below

2
On

Since you create the AttachmentsRadListBox dynamically, do you recreate it on subsequent postbacks? It is, in the end, a server control, so you need to make sure it is recreated, because otherwise ASP will destroy it upon a subsequent postback. To see how you can access controls in the ContentTemplate of a RadWindow you can also examine this article: http://www.telerik.com/help/aspnet-ajax/window-controls-container.html.