Datagrid ItemCommand event not firing

mark007

Centurion
Joined
Jan 22, 2005
Messages
185
Location
York, UK
I have the following code:

Code:
<asp:DataGrid id="dgData" runat="server" Visible="False" HorizontalAlign="Center" BorderStyle="Solid"
					BorderColor="Black" CellPadding="3" PageSize="50" AutoGenerateColumns="False">
					<SelectedItemStyle BackColor="#C0C0FF"></SelectedItemStyle>
					<AlternatingItemStyle ForeColor="Blue" BackColor="White"></AlternatingItemStyle>
					<HeaderStyle Font-Bold="True" BackColor="#E0E0E0"></HeaderStyle>
					<PagerStyle Mode="NumericPages"></PagerStyle>
				</asp:DataGrid>


Code:
Me.dgData.Columns.Clear()
        Dim cBound As System.Web.UI.WebControls.BoundColumn
        cBound = New System.Web.UI.WebControls.BoundColumn
        cBound.DataField = "ID"
        cBound.HeaderText = "ID"
        cBound.Visible = False
        Me.dgData.Columns.Add(cBound)
        cBound = New System.Web.UI.WebControls.BoundColumn
        cBound.DataField = "TaxYear"
        cBound.HeaderText = "Tax Year"
        Me.dgData.Columns.Add(cBound)
        cBound = New System.Web.UI.WebControls.BoundColumn
        cBound.DataField = "LEL"
        cBound.HeaderText = "LEL"
        Me.dgData.Columns.Add(cBound)
        cBound = New System.Web.UI.WebControls.BoundColumn
        cBound.DataField = "UEL"
        cBound.HeaderText = "UEL"
        Me.dgData.Columns.Add(cBound)
        If blEdit Then
            cBound = New System.Web.UI.WebControls.BoundColumn
            cBound.DataField = "Doer"
            cBound.HeaderText = "Doer"
            Me.dgData.Columns.Add(cBound)
            cBound = New System.Web.UI.WebControls.BoundColumn
            cBound.DataField = "Checker"
            cBound.HeaderText = "Checker"
            Me.dgData.Columns.Add(cBound)
            AddButtons()
        End If
        'get data
        Dim ds As System.Data.DataSet
        ds = Me.DB.GetData("select * from LELandUEL")
        Me.lbTitle.Text = "Here is the data for Earnings Limits:"
        Me.dgData.DataSource = ds
AddHandler dgData.ItemCommand, AddressOf Me.dgData_ItemCommand
        Me.dgData.DataBind()

Code:
Private Sub AddButtons()
        Dim cEdit As System.Web.UI.WebControls.EditCommandColumn
        cEdit = New System.Web.UI.WebControls.EditCommandColumn
        With cEdit
            .ButtonType = ButtonColumnType.LinkButton
            .CancelText = "Cancel"
            .EditText = "Edit"
            .HeaderText = "Edit"
            .UpdateText = "Update"
        End With
        Me.dgData.Columns.Add(cEdit)
        Dim cDel As New System.Web.UI.WebControls.ButtonColumn
        With cDel
            .ButtonType = ButtonColumnType.LinkButton
            .CommandName = "Delete"
            .HeaderText = "Delete"
            .Text = "Delete"
        End With
        Me.dgData.Columns.Add(cDel)
    End Sub

   Private Sub dgData_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        Select Case e.CommandName
            Case "Edit"
                Me.dgData.EditItemIndex = e.Item.ItemIndex
                BindData()
            Case "Cancel"
                Me.dgData.EditItemIndex = -1
                BindData()
            Case "Delete"

            Case "Update"
        End Select

    End Sub

The event is not firing. When I specifoed the columns in the aspx file and used autognerate for the bound columns it worked fine but with this dynamoc setup it doesn't work!

Trace info if it helps:

aspx.page Begin Init
aspx.page End Init 0.000047 0.000047
aspx.page Begin LoadViewState 0.000086 0.000039
aspx.page End LoadViewState 0.001499 0.001413
aspx.page Begin ProcessPostData 0.001557 0.000058
aspx.page End ProcessPostData 0.001591 0.000034
aspx.page Begin ProcessPostData Second Try 0.001623 0.000033
aspx.page End ProcessPostData Second Try 0.001652 0.000029
aspx.page Begin Raise ChangedEvents 0.001681 0.000028
aspx.page End Raise ChangedEvents 0.001711 0.000030
aspx.page Begin Raise PostBackEvent 0.001742 0.000031
aspx.page End Raise PostBackEvent 0.001788 0.000045
aspx.page Begin PreRender 0.001820 0.000032
aspx.page End PreRender 0.001854 0.000035
aspx.page Begin SaveViewState 0.002405 0.000550
aspx.page End SaveViewState 0.002502 0.000097
aspx.page Begin Render 0.002540 0.000038
aspx.page End Render 0.003090 0.000550


__EVENTTARGET dgData:_ctl4:_ctl0
__EVENTARGUMENT
__VIEWSTATE dDw0NTU3OTI4ODI7dDw7bDxpPDE+Oz47bDx0PDtsPGk8MT47aTwzPjs+O2w8dDxwPHA8bDxWaXNpYmxlOz47bDxvPHQ+Oz4+Oz47bDxpPDE+O2k8Mz47PjtsPHQ8cDxwPGw8VGV4dDs
 
Ok, so I managed to find a work around that worked by specifying the edit command button in the aspx html and the hiding it in code rather than adding it. However now when I come to do the Update using:

Dim dDate As Date = System.Convert.ToDateTime(CType(e.Item.Cells(3).Controls(0), TextBox).Text)

.cells(2) doesn't exist. In fact the only cell that exists is my one column specified in the aspx file for the editcommandbutton.

Is there a way round this? or is the control just not made for adding columns at RT?

:)
 
OK, for anyone else coming across the same problem the way to do it is to include the code that sets up the bound columns in Page_Load everytime not just when you wish to databind.

:)
 
Back
Top