Pre ticking checkboxes on load

6 posts, 1 answers
  1. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 07 Aug 2012 Link to this post

    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


  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Aug 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 08 Aug 2012 Link to this post

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

    Posted 09 Aug 2012 Link to this post

    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.
  6. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 10 Aug 2012 Link to this post

    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
  7. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 10 Aug 2012 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017