Reputation: 179
I have a gridview control that is dynamically configured after a button-click event. Some of the columns contain dynamically added checkboxes. For some reason, I can't get the OnCheckedChanged event to fire for any of the checkboxes in this gridview.
Here's what fires after the button click event:
Private Sub BuildGridViewColumnList()
Try
' Clear all columns.
grdCommodityConfig.Columns.Clear()
' Add static columns.
Dim CommodityColumn As New BoundField
CommodityColumn.HeaderText = "Commodity"
CommodityColumn.DataField = "Commodity"
grdCommodityConfig.Columns.Add(CommodityColumn)
Dim PartTypeColumn As New BoundField
PartTypeColumn.HeaderText = "Part Type"
PartTypeColumn.DataField = "PartType"
grdCommodityConfig.Columns.Add(PartTypeColumn)
' Add dynamic columns
Dim ColumnHeaders As String = String.Empty
Database.GetCommodityConfig(txtAssyLine.Text, ColumnHeaders)
Dim ColumnList As List(Of String) = ColumnHeaders.Split(New Char() {","c}).ToList
' Add each column found in list returned from DB.
For Each ColumnName As String In ColumnList
Dim ItemTmpField As New TemplateField()
' create HeaderTemplate
ItemTmpField.HeaderTemplate = New DynamicallyTemplatedGridViewHandler(ListItemType.Header, ColumnName, "CheckBox")
' create ItemTemplate
ItemTmpField.ItemTemplate = New DynamicallyTemplatedGridViewHandler(ListItemType.Item, ColumnName, "CheckBox")
'create EditItemTemplate
ItemTmpField.EditItemTemplate = New DynamicallyTemplatedGridViewHandler(ListItemType.EditItem, ColumnName, "CheckBox")
' then add to the GridView
ItemTmpField.ItemStyle.HorizontalAlign = HorizontalAlign.Center
grdCommodityConfig.Columns.Add(ItemTmpField)
Next
Catch ex As Exception
Throw ex
End Try
End Sub
This is the class used to add the gridview & checkboxes:
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Specialized
Imports System.Data.SqlClient
Public Class DynamicallyTemplatedGridViewHandler
Implements ITemplate
Private ItemType As ListItemType
Private FieldName As String
Private InfoType As String
Public Sub New(item_type As ListItemType, field_name As String, info_type As String)
ItemType = item_type
FieldName = field_name
InfoType = info_type
End Sub
Public Sub InstantiateIn(Container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
Select Case ItemType
Case ListItemType.Header
Dim header_ltrl As New Literal()
header_ltrl.Text = "<b>" & FieldName & "</b>"
Container.Controls.Add(header_ltrl)
Exit Select
Case ListItemType.Item
Select Case InfoType
Case "CheckBox"
' for other 'non-command' i.e. the key and non key fields, bind textboxes with corresponding field values
Dim field_chkbox As New CheckBox()
field_chkbox.ID = FieldName
field_chkbox.Text = [String].Empty
' if Inert is intended no need to bind it with text..keep them empty
AddHandler field_chkbox.DataBinding, New EventHandler(AddressOf OnDataBinding)
AddHandler field_chkbox.CheckedChanged, New EventHandler(AddressOf OnCheckedChanged)
field_chkbox.CausesValidation = False
Container.Controls.Add(field_chkbox)
Case Else
Dim field_lbl As New Label()
field_lbl.ID = FieldName
field_lbl.Text = [String].Empty
'we will bind it later through 'OnDataBinding' event
AddHandler field_lbl.DataBinding, New EventHandler(AddressOf OnDataBinding)
Container.Controls.Add(field_lbl)
Exit Select
End Select
Exit Select
Case ListItemType.EditItem
If InfoType = "CheckBox" Then
' for other 'non-command' i.e. the key and non key fields, bind textboxes with corresponding field values
Dim field_chkbox As New CheckBox()
field_chkbox.ID = FieldName
field_chkbox.Text = [String].Empty
AddHandler field_chkbox.DataBinding, New EventHandler(AddressOf OnDataBinding)
AddHandler field_chkbox.CheckedChanged, New EventHandler(AddressOf OnCheckedChanged)
field_chkbox.CausesValidation = False
Container.Controls.Add(field_chkbox)
Else
' for other 'non-command' i.e. the key and non key fields, bind textboxes with corresponding field values
Dim field_txtbox As New TextBox()
field_txtbox.ID = FieldName
field_txtbox.Text = [String].Empty
AddHandler field_txtbox.DataBinding, New EventHandler(AddressOf OnDataBinding)
Container.Controls.Add(field_txtbox)
End If
Exit Select
End Select
End Sub
Private Sub OnDataBinding(sender As Object, e As EventArgs)
Dim bound_value_obj As Object = Nothing
Dim ctrl As Control = DirectCast(sender, Control)
Dim data_item_container As IDataItemContainer = DirectCast(ctrl.NamingContainer, IDataItemContainer)
bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName)
Select Case ItemType
Case ListItemType.Item
Dim field_ltrl As CheckBox = DirectCast(sender, CheckBox)
field_ltrl.Checked = CBool(bound_value_obj.ToString())
AddHandler field_ltrl.CheckedChanged, New EventHandler(AddressOf OnCheckedChanged)
field_ltrl.CausesValidation = False
Exit Select
Case ListItemType.EditItem
Dim field_txtbox As CheckBox = DirectCast(sender, CheckBox)
field_txtbox.Checked = CBool(bound_value_obj.ToString())
AddHandler field_txtbox.CheckedChanged, New EventHandler(AddressOf OnCheckedChanged)
field_txtbox.CausesValidation = False
Exit Select
End Select
End Sub
Upvotes: 0
Views: 673
Reputation: 179
I believe I've found the answer to my own question. After some more Google searches, I added the following lines of code, and the CheckChanged event finally started firing. It's a step in the right direction!
Added this beneath the AddHandler statements in the DynamicallyTemplatedGridViewHandler class:
field_chkbox.AutoPostBack = True
As suggested by Tim S, I added the following in the page that houses the dynamic gridview control:
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Me.IsPostBack Then
Try
' Need to build the column list dynamically.
BuildGridViewColumnList()
' Refresh GridView data.
BindGridViewData()
SetErrorMessage(String.Empty)
Catch ex As Exception
SetErrorMessage(ex.Message)
End Try
End If
End Sub
Upvotes: 0