0

我有一个在按钮单击事件后动态配置的 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

这是用于添加网格视图和复选框的类:

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
4

1 回答 1

0

我相信我已经找到了自己问题的答案。在谷歌搜索了一些之后,我添加了以下代码行,CheckChanged 事件终于开始触发。这是朝着正确方向迈出的一步!

在 DynamicallyTemplatedGridViewHandler 类的 AddHandler 语句下添加了这个:

field_chkbox.AutoPostBack = True

正如 Tim S 所建议的,我在包含动态 gridview 控件的页面中添加了以下内容:

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
于 2014-10-06T17:56:54.317 回答