lundi 23 novembre 2015

Uncheck last checkbox on CheckedChanged

I have a gridview with an itemtemplate column with a Checkbox in it. I only want the user to select 3 of these checkboxes. On the 4 selection I want to pop up a message telling them only 3 are allowed, and then uncheck the check box they just checked. I tried different javascripts, but couldn't get it to work.

Now I'm very close to doing it in my vb code behind on the the CheckedChanged event. I can pop up the message on the 4th selection, but I can't uncheck the box. I thought box.Checked = False would do it, but it doesn't. Do I need to do something with the Sender? Or am I close with what I have?

Protected Sub CheckBoxPain_CheckedChanged(sender As Object, e As System.EventArgs)

    Dim box As CheckBox
    Dim numChecked As Integer = 0

    For Each row As GridViewRow In gv_pain.Rows
        If row.RowType = DataControlRowType.DataRow Then
            box = CType(row.Cells(0).FindControl("CheckBoxPain"), CheckBox)
            If box.Checked Then
                numChecked = numChecked + 1
            End If
        End If
    Next

    'Response.Write("NUM:" & numChecked.ToString)
    'Response.Write("STATUS:" & box.Checked.ToString)
    'Response.Write("ITEM:" & box.SelectedItem.Text)

    If numChecked.ToString > 3 Then
        Response.Write("<script>alert('Only 3 Pain Selections allowed')</script>")
        box.Checked = False
    End If

End Sub




Aucun commentaire:

Enregistrer un commentaire