CheckedChanged事件没有使用动态复选框触发?

时间:2014-10-06 14:33:50

标签: vb.net gridview itemplate

我有一个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

1 个答案:

答案 0 :(得分: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