back up codes

always have a backup of your code

Archive for the tag “checkbox”

determines if checkbox control inside datalist is checked

The code determines if checkbox control inside datalist is checked or unchecked, then do something depending on the status of the checkbox. It will also get the value of the chosen data.

The following code can be inserted on any control event:

Dim list As DataListItem
For Each list In myDataList.Items
Dim thechkbx As CheckBox = list.FindControl("myCheckBox")
If thechkbx IsNot Nothing AndAlso thechkbx.Checked AndAlso thechkbx.Enabled Then
Dim myDataKey As String = Me.myDataList.DataKeys(list.ItemIndex).ToString()
(do something here)
Else
(do something here)
End If
Next

determines if checkbox inside gridview is checked

The following code determines if checkbox control inside gridview is checked or unchecked.


For Each myRow As GridViewRow In myGridview.Rows
Dim chkbx As CheckBox = myRow.FindControl("chkbxCheck")
If chkbx IsNot Nothing AndAlso chkbx.Checked AndAlso chkbx.Enabled Then
(do something here if checkbox is checked)
Else
(do something here if checkbox is not checked)
End If
Next

enable/disable checkbox inside gridview

This code will check/uncheck and enable/disable a checkbox inside a gridview, depending if data is already found on database.

Insert the following code on the DataBound event of the gridview control:

For Each myRow As GridViewRow In myGrid.Rows
Dim thechkbx As CheckBox = CType(myRow.FindControl("myChkbx"), CheckBox)
Dim myID As String = myGrid.DataKeys(myRow.RowIndex).Value
command = New Data.SqlClient.SqlCommand("SELECT * FROM MY_TABLE WHERE MY_PK = '" & myID & "'", connection)
reader = command.ExecuteReader()
If reader.Read() Then
thechkbx.Enabled = True
thechkbx.Checked = True
Else
thechkbx.Enabled = False
thechkbx.Checked = False
End If
reader.Close()
Next

Post Navigation

Follow

Get every new post delivered to your Inbox.