This is a migrated thread and some comments may be shown as answers.

Accessing RadListBox Items in Code-Behind

7 Answers 399 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
B
Top achievements
Rank 1
B asked on 31 Jul 2013, 04:09 PM
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.


7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 01 Aug 2013, 07:47 AM
Hi B,

Please have a look into the following code I tried which works fine at my end.

ASPX:
<telerik:RadWindow ID="RadWindow1" runat="server" Width="450px" Height="450px">
    <ContentTemplate>
        <telerik:RadListBox ID="AttachmentsRadListBox" CheckBoxes="true" runat="server" />
        <br />
        <br />
        <telerik:RadButton ID="RadButton2" runat="server" Text="Save" OnClick="RadButton2_Click">
        </telerik:RadButton>
    </ContentTemplate>
</telerik:RadWindow>
<br />
<telerik:RadButton ID="RadButton1" runat="server" Text="Open RadWindow" OnClick="RadButton1_Click">
</telerik:RadButton>
<br />
<asp:Label ID="Label1" runat="server" Text="UnChecked Items are : "></asp:Label>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="select top 5 ItemName from [ItemList]"></asp:SqlDataSource>

C#:
protected void RadButton1_Click(object sender, EventArgs e)
{
    RadWindow1.VisibleOnPageLoad = true;
    AttachmentsRadListBox.DataSource = SqlDataSource1;
    AttachmentsRadListBox.DataTextField = "ContactName";
    AttachmentsRadListBox.DataBind();
    foreach (RadListBoxItem item in AttachmentsRadListBox.Items)
    {
        item.Checked = true;
    }
}
 
protected void RadButton2_Click(object sender, EventArgs e)
{
    foreach(RadListBoxItem item in AttachmentsRadListBox.Items)
    {
        if (item.Checked == false)
        {
            string documentIDToDelete = item.Value;
            Label1.Text += documentIDToDelete + " ";
        }
    }
}

Thanks,
Shinu.
0
B
Top achievements
Rank 1
answered on 02 Aug 2013, 01:14 AM
Hi Shinu,

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!
0
Nencho
Telerik team
answered on 05 Aug 2013, 08:15 AM
Hello D B,

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?

Regards,
Nencho
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
B
Top achievements
Rank 1
answered on 06 Aug 2013, 07:30 PM
Well, the configuration is really what I explained in the first explanation I had. From what I understand, I created the RadListBox on the Server side (Through setting the DataSource of it to a DataTable) therefore after pressing Save Button there is a Post back which I believe makes the RadListBox I created to go away. That's why I have access to RadListBox in the following piece of code:
 
AttachmentsRadListBox.DataSource = AttachDT ' A DataTable coming from a SQL query
AttachmentsRadListBox.DataTextField = "DocumentPath"
AttachmentsRadListBox.DataValueField = "DocumentID"
AttachmentsRadListBox.DataBind()
For Each item As RadListBoxItem In AttachmentsRadListBox.Items
    item.Checked = True
Next
 But then in the method that handles Save Button Press, 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
 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?
0
Princy
Top achievements
Rank 1
answered on 08 Aug 2013, 01:14 PM
Hi B,

Please check the following code snippet I tried which works fine as expected.

ASPX:
<telerik:RadWindow ID="RadWindow1" runat="server" Width="450px" Height="450px">
    <ContentTemplate>
        <telerik:RadListBox ID="AttachmentsRadListBox" CheckBoxes="true" runat="server" />
        <br />
        <br />
        <telerik:RadButton ID="RadButton2" runat="server" Text="Save" OnClick="RadButton2_Click">
        </telerik:RadButton>
    </ContentTemplate>
</telerik:RadWindow>
<br />
<telerik:RadButton ID="RadButton1" runat="server" Text="Display RadWindow" OnClick="RadButton1_Click">
</telerik:RadButton>
<br />
<asp:Label ID="lbl1" runat="server" Text="UnChecked Items are : "></asp:Label>

C#:
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
 
    return myDataTable;
}
 
protected void RadButton1_Click(object sender, EventArgs e)
{
    RadWindow1.VisibleOnPageLoad = true;
    DataTable datatable1 = GetDataTable("SELECT CustomerID FROM Customers");
    AttachmentsRadListBox.DataSource = datatable1;
    AttachmentsRadListBox.DataTextField = "CustomerID";
    AttachmentsRadListBox.DataBind();
    foreach (RadListBoxItem item in AttachmentsRadListBox.Items)
    {
        item.Checked = true;
    }
}
 
protected void RadButton2_Click(object sender, EventArgs e)
{
    foreach(RadListBoxItem item in AttachmentsRadListBox.Items)
    {
        if (item.Checked == false)
        {
            string documentIDToDelete = item.Value;
            lbl1.Text += documentIDToDelete + " ";
        }
    }
}

Thanks,
Princy.
0
Thomas
Top achievements
Rank 1
answered on 20 Aug 2015, 12:59 PM

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?

0
Thomas
Top achievements
Rank 1
answered on 20 Aug 2015, 01:26 PM
Ignore my last post - somehow my DataValueField setting had been deleted (which was causing my problems reading the Item.Value and Item.DataKey problem).
Tags
ListBox
Asked by
B
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
B
Top achievements
Rank 1
Nencho
Telerik team
Princy
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Share this question
or