Accessing RadListBox Items in Code-Behind

8 posts, 0 answers
  1. B
    B avatar
    3 posts
    Member since:
    Jul 2013

    Posted 31 Jul 2013 Link to this post

    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.


  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 01 Aug 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. B
    B avatar
    3 posts
    Member since:
    Jul 2013

    Posted 01 Aug 2013 Link to this post

    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!
  5. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 05 Aug 2013 Link to this post

    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.
  6. B
    B avatar
    3 posts
    Member since:
    Jul 2013

    Posted 06 Aug 2013 Link to this post

    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?
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Aug 2013 Link to this post

    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.
  8. Thomas
    Thomas avatar
    2 posts
    Member since:
    Apr 2014

    Posted 20 Aug 2015 in reply to Princy Link to this post

    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?

  9. Thomas
    Thomas avatar
    2 posts
    Member since:
    Apr 2014

    Posted 20 Aug 2015 in reply to Thomas Link to this post

    Ignore my last post - somehow my DataValueField setting had been deleted (which was causing my problems reading the Item.Value and Item.DataKey problem).
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017