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

Pre ticking checkboxes on load

5 Answers 123 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Andy Green
Top achievements
Rank 2
Andy Green asked on 07 Aug 2012, 02:00 PM
I have a list in a RadDropDown, that users can select multiple values from, these are then saved to a database in a comma separated string.

When I wish to edit the data I need the same checkboxes to be ticked.

My code is atttached, I'm getting the first value to tick, but on subseqent values the x is null and no further binding takes place. The value of the selectedItem matched the value of Usr from the array.

'populate the dropdown and pre select checkboxes
If Not IsDBNull(u_ds.Tables(0).Rows(0)("LinkedUsers")) Then
    Dim arrLinkedUsers As Array = Split(u_ds.Tables(0).Rows(0)("LinkedUsers"), ",")
    For Each Usr In arrLinkedUsers
 
        ddlClinic.SelectedValue = Usr
        Dim x As RadComboBoxItem = DirectCast(ddlClinic.SelectedItem, RadComboBoxItem)
        If Not x Is Nothing Then
            x.Checked = True
        End If
 
    Next Usr
End If

Andy


5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 Aug 2012, 04:56 AM
Hi,

Try the following code to achieve your scenario.

VB:
Dim data As String = dt.Rows(0)("LinkedUsers").ToString()
If data.Length > 0 Then
 Dim arrLinkedUsers As String()
 arrLinkedUsers = data.Split(","C)
 For Each Usr As String In arrLinkedUsers
  If RadComboBox1.Items.FindItemByValue(Usr) IsNot Nothing Then
   RadComboBox1.Items.FindItemByValue(Usr).Checked = True
  End If
 Next
End If

Hope this helps.

Thanks,
Princy.
0
Andy Green
Top achievements
Rank 2
answered on 08 Aug 2012, 06:54 AM
Thanks Princy, but this is the same. There are for example 3 items in the array, these values are also in the dropdown as values, but it will only ever tick the first one it comes to.

This is my markup:

 

<telerik:RadComboBox ID="ddlClinician" Runat="server" Width="245px" EnableEmbeddedSkins="false" Skin="Activity" CheckBoxes="true" emptyMessage="&nbsp;"></telerik:RadComboBox>


If data.Length > 0 Then
    Dim arrLinkedUsers As String()
    arrLinkedUsers = data.Split(","c)
    For Each Usr As String In arrLinkedUsers
        If ddlClinician.Items.FindItemByValue(Usr) IsNot Nothing Then
            ddlClinician.Items.FindItemByValue(Usr).Checked = True
        End If
    Next
End If

This is the line that only returns true for the first item in the array -

 

If ddlClinician.Items.FindItemByValue(Usr) IsNot Nothing Then


Andy
0
Princy
Top achievements
Rank 2
answered on 09 Aug 2012, 06:21 AM
Hi,

Please modify the code as follows to check every radcombobox items having the values in the array.

VB:
If data.Length > 0 Then
    Dim arrLinkedUsers As String()
    arrLinkedUsers = data.Split(","C)
    For Each Usr As String In arrLinkedUsers
        Dim selected As IEnumerable(Of RadComboBoxItem) = ddlClinician.Items.Where(Function(item) item.Value = Usr)
        For Each item As RadComboBoxItem In selected
            item.Checked = True
        Next
    Next
End If

Thanks,
Princy.
0
Andy Green
Top achievements
Rank 2
answered on 10 Aug 2012, 07:11 AM
Still the same,it only finds the first in the list.

Where should this code be run, My current set up populates the drop down list with data on a page load, and I have a function that gets the user record, and currently its here that I'm trying to re populate the checkbox ticks.

Andy
0
Andy Green
Top achievements
Rank 2
answered on 10 Aug 2012, 07:22 AM
Hi Princy

This is now working and it looks like it was my fault, and sorry for wasting your time.

On closer inspection of the data, there was a leading space, which of course wouldn't match up witht he list data.

Andy
Tags
ComboBox
Asked by
Andy Green
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Andy Green
Top achievements
Rank 2
Share this question
or