lundi 21 juin 2021

Add text to checkboxlist item being added to another checkboxlist (while preserving original checkboxlist item)

I'm using Visual Studio, vb.net, and asp.net with webforms.

I have two checkboxlists, one called Available and one called Modifications. I don't want (Add) to show where I have the blue arrow in this picture. Please view this image for clarity on what I am about to explain.

When a user checks some items in Available and clicks the "Add" button, I have code to add the item to the Modifications checkboxlist. The item does not leave the Available checkboxlist until it hits the account max.

For example, Available Checkboxlist item Membership is allowed to be added twice. The user checks Membership and clicks Add. It adds to the Modifications checkboxlist. Membership remains in the Available checkboxlist. The user checks Membership again and clicks Add, and Membership is added a second time to Modifications. This time, Membership is removed from the Available checkboxlist because they hit the max of 2. (This number can be any number I set it to, by the way).

My issue is, when I add the item to Modifications, I want to concatenate the word (Add) to the item. However it also adds the word (Add) to my Available checkboxlist. I want to say (Add) in modifications but not in available. How can I do this? This is where I do it CBLAvailable.Items(i).Text = CBLAvailable.Items(i).Text + serviceAdd where serviceAdd is Private serviceAdd As String = "<span style='color:red;'><em> (Add)</em></span>"

  Protected Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click

        'Create the local DataTable and set it to the ViewState
        Dim dtEditedAvailable As DataTable
        dtEditedAvailable = DirectCast(ViewState("vsEditedAvailable"), DataTable)

        'Create a list to hold services that need removed
        Dim removeAvailableService As New List(Of ListItem)()

        'ADD ITEM FROM AVAILABLE TO MODIFICATION

        'Loop through each service in CBLAvailable
        For i As Integer = 0 To CBLAvailable.Items.Count - 1

            'If a service in CBLAvailable is selected
            If CBLAvailable.Items(i).Selected = True Then

                'Uncheck the item
                CBLAvailable.Items(i).Selected = False

                'Add this CBLAvailable service item to CBLModifications
                CBLAvailable.Items(i).Text = CBLAvailable.Items(i).Text + serviceAdd
                CBLModifications.Items.Add(CBLAvailable.Items(i))
                'Dim serviceValue = CBLAvailable.Items(i).Value
                'Dim serviceMod = CBLModifications.Items.FindByValue(serviceValue)
                'serviceMod.Text += serviceAdd

                'Get the service ID from CBLAvailable and select the matching service row in dtAvailable
                Dim serviceId = CBLAvailable.Items(i).Value
                Dim serviceRows() As DataRow = dtEditedAvailable.Select("ID = " + serviceId)

                If serviceRows.Count > 0 Then
                    'Get the AccountTotal (Number of accounts user added)
                    Dim serviceAccountTotal = serviceRows(0).Item("AccountTotal").ToString()

                    'Add one to the AccountTotal
                    Dim newTotal = Integer.Parse(serviceAccountTotal) + 1

                    serviceRows(0).Item("AccountTotal") = newTotal
                    dtEditedAvailable.AcceptChanges()

                    'Compare to the AccountMax and add the item from CBLAvailable to removeAvailableService if max is reached
                    Dim serviceAccountMax = Integer.Parse(serviceRows(0).Item("MaxAccount").ToString())
                    If newTotal >= serviceAccountMax Then
                        removeAvailableService.Add(CBLAvailable.Items(i))
                    End If
                End If
            End If
        Next

        'REMOVE SERVICE ITEM FROM AVAILABLE
        'Loop through each service in the removeAvailableService list that needs to be removed
        For i As Integer = 0 To removeAvailableService.Count - 1

            'Remove the service from CBLAvailable
            CBLAvailable.Items.Remove(removeAvailableService(i))

        Next

        'Set the current state of the DataTable to the ViewState
        ViewState("vsEditedAvailable") = dtEditedAvailable
End Sub



Aucun commentaire:

Enregistrer un commentaire