T
Terry Holland
I have created a custom control that dynamically creates a datagrid at
runtime. The datagrid is made up of a collection of custom columns. One of
my custom columns is a Row Selecter column. This comprises a Checkbox for
each row and one in the header of datagrid.
My row selection is working fine and I am able to loop through the selected
items in my grid and perform an action on them. What I want to be able to
do now is give the user the ability to select or un-select all rows by
clicking the checkbox in the header.
The trouble that I am experiencing is that the CheckChanged event of the
checkbox in the header only fires if the value changes from the value that
it is when it is dynamically created.
Each postback causes my custom control to be built. When it is built the
row selector column (clsDGCCheckBoxColumn) is added and the InitializeCell
method adds a checkbox to the header column with the checked value = False.
When the user checks this control, the SelectColumn method runs correctly.
If the user unchecks the checkbox in the header, the SelectColumn method
does not run. Im assuming that this is because the value of this control
when it is built is False and when the user unchecks the control it is once
again False, therefore the value has not changed therefore the event does
not fire.
Could someone advise me how to get around this problem - I guess that I need
to modify InitializeCell so that the value that I set the checkbox to is as
it was before postback.
Thanks
Terry Holland
##############################
Custom CheckBox Column
##############################
Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports System.ComponentModel
Public Class clsDGCCheckBoxColumn
Inherits clsCustomDGC_BASE
Public Sub New(ByVal objDGC As clsDataGridColumn)
MyBase.New(objDGC, "chk")
m_clrBackColor = System.Drawing.Color.LightCyan
End Sub
Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
Select Case itemType
Case ListItemType.Header
Dim ctl As CheckBox = New CheckBox
ctl.AutoPostBack = True
ctl.Checked = False
cell.Controls.Add(ctl)
AddHandler ctl.CheckedChanged, AddressOf SelectColumn '(Me,
ctl.Checked)
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem, ListItemType.SelectedItem
Dim ctl As CheckBox = New CheckBox
SetControlProperties(cell, ctl)
cell.Controls.Add(ctl)
AddHandler cell.DataBinding, AddressOf Cell_DataBinding
Case Else
End Select
End Sub
Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim cell As TableCell = sender
Dim item As DataGridItem = cell.NamingContainer
Dim ctl As CheckBox = item.FindControl(m_strID)
Dim pd As PropertyDescriptor =
TypeDescriptor.GetProperties(item.DataItem).Find(m_strDataField, True)
Try
Dim obj1 As Object = pd.GetValue(item.DataItem)
ctl.Checked = CType(obj1, Boolean)
Catch ex As Exception
Throw New Exception("Invalid dataField for custom column
clsDGCLabel")
End Try
End Sub
Private Sub SelectColumn(ByVal sender As Object, ByVal e As EventArgs) '
ByVal booSelect As Boolean)
Dim ctlHeader As CheckBox = sender
Dim grid As DataGrid = ctlHeader.NamingContainer.NamingContainer
Dim booSelect As Boolean = ctlHeader.Checked
Dim o As CheckBox
For Each dgi As DataGridItem In grid.Items
o = dgi.FindControl(m_strID)
o.Checked = booSelect
Next
End Sub
End Class
runtime. The datagrid is made up of a collection of custom columns. One of
my custom columns is a Row Selecter column. This comprises a Checkbox for
each row and one in the header of datagrid.
My row selection is working fine and I am able to loop through the selected
items in my grid and perform an action on them. What I want to be able to
do now is give the user the ability to select or un-select all rows by
clicking the checkbox in the header.
The trouble that I am experiencing is that the CheckChanged event of the
checkbox in the header only fires if the value changes from the value that
it is when it is dynamically created.
Each postback causes my custom control to be built. When it is built the
row selector column (clsDGCCheckBoxColumn) is added and the InitializeCell
method adds a checkbox to the header column with the checked value = False.
When the user checks this control, the SelectColumn method runs correctly.
If the user unchecks the checkbox in the header, the SelectColumn method
does not run. Im assuming that this is because the value of this control
when it is built is False and when the user unchecks the control it is once
again False, therefore the value has not changed therefore the event does
not fire.
Could someone advise me how to get around this problem - I guess that I need
to modify InitializeCell so that the value that I set the checkbox to is as
it was before postback.
Thanks
Terry Holland
##############################
Custom CheckBox Column
##############################
Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports System.ComponentModel
Public Class clsDGCCheckBoxColumn
Inherits clsCustomDGC_BASE
Public Sub New(ByVal objDGC As clsDataGridColumn)
MyBase.New(objDGC, "chk")
m_clrBackColor = System.Drawing.Color.LightCyan
End Sub
Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
Select Case itemType
Case ListItemType.Header
Dim ctl As CheckBox = New CheckBox
ctl.AutoPostBack = True
ctl.Checked = False
cell.Controls.Add(ctl)
AddHandler ctl.CheckedChanged, AddressOf SelectColumn '(Me,
ctl.Checked)
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem, ListItemType.SelectedItem
Dim ctl As CheckBox = New CheckBox
SetControlProperties(cell, ctl)
cell.Controls.Add(ctl)
AddHandler cell.DataBinding, AddressOf Cell_DataBinding
Case Else
End Select
End Sub
Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim cell As TableCell = sender
Dim item As DataGridItem = cell.NamingContainer
Dim ctl As CheckBox = item.FindControl(m_strID)
Dim pd As PropertyDescriptor =
TypeDescriptor.GetProperties(item.DataItem).Find(m_strDataField, True)
Try
Dim obj1 As Object = pd.GetValue(item.DataItem)
ctl.Checked = CType(obj1, Boolean)
Catch ex As Exception
Throw New Exception("Invalid dataField for custom column
clsDGCLabel")
End Try
End Sub
Private Sub SelectColumn(ByVal sender As Object, ByVal e As EventArgs) '
ByVal booSelect As Boolean)
Dim ctlHeader As CheckBox = sender
Dim grid As DataGrid = ctlHeader.NamingContainer.NamingContainer
Dim booSelect As Boolean = ctlHeader.Checked
Dim o As CheckBox
For Each dgi As DataGridItem In grid.Items
o = dgi.FindControl(m_strID)
o.Checked = booSelect
Next
End Sub
End Class