我有一个gridview控件,它是在按钮单击事件后动态配置的。某些列包含动态添加的复选框。由于某些原因,我无法为此网格视图中的任何复选框激发OnCheckedChanged事件。
以下是按钮点击事件后发生的情况:
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
这是用于添加gridview&复选框:
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
我相信我已经找到了自己问题的答案。经过更多的谷歌搜索,我添加了以下几行代码,CheckChanged事件终于开始启动了。这是朝着正确方向迈出的一步!
在DynamicallyTemplatedGridViewHandler类的AddHandler语句下面添加了这一点:
field_chkbox.AutoPostBack = True
根据Tim S的建议,我在包含动态网格视图控件的页面中添加了以下内容:
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