0

I use a DataGrid only programmatically.

 Public Class MyOwnDataGrid
    Inherits MyBaseClass

    Private DataControl As New WebControls.DataGrid
    Public Property DataSource as DataTable = Nothing

    Public Sub New()
        AddHandler Me.DataControl.ItemCommand, AddressOf Me.DataGrid_ItemCommand
        Me.DataControl.AutoGenerateColumns = False
    End Sub
 End Class

On Page_Load I bind the DataSource and add the columns.

Protected Overrides Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
  'I have to hold the DataSource in a Session variable because after PostBack is the DataSource property nothing
  If (Not (Me.DataSource Is Nothing) And Not (Me.IsPostBack)) Then Session("DataSource") = Me.DataSource

  If (Not (Session(Me.DataSourceSessionName) Is Nothing)) Then
    Me.DataControl.DataSource = Session("DataSource")
    Me.DataControl.DataSource.AcceptChanges()
  End If

  'add the columns with content

  'add the columns for Actions

  Dim oColumn As New TemplateColumn
  oColumn.ItemTemplate = New ActionColumn With {.Control = New WebControls.ImageButton With {.SkinID = "Edit", .ToolTip = "Edit", .CommandName = "Edit"}}
  oColumn.EditItemTemplate = New ActionColumn With {.Control = New WebControls.ImageButton With {.SkinID = "Update", .ToolTip = "Apply", .CommandName = "Update"}}
  oColumn.FooterTemplate = New ActionColumn With {.Control = New WebControls.ImageButton With {.SkinID = "Insert", .ToolTip = "Add", .CommandName = "Insert"}}
  Me.DataControl.Columns.Add(oColumn)

  oColumn = New TemplateColumn
  oColumn.ItemTemplate = New ActionColumn With {.Control = New WebControls.ImageButton With {.SkinID = "Delete", .ToolTip = "Delete", .CommandName = "Delete"}}
  oColumn.EditItemTemplate = New ActionColumn With {.Control = New WebControls.ImageButton With {.SkinID = "Cancel", .ToolTip = "Cancel", .CommandName = "Cancel"}}
  Me.DataControl.Columns.Add(oColumn)

  'Bind the DataSource at every PostBack because otherwise the DataSource is empty and the control will be hidden

  Me.DataControl.DataBind()
End Sub

Private Class ActionColumn
  Implements System.Web.UI.ITemplate

  Public Property Control As WebControls.ImageButton = Nothing

  Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
    If (Not (Me.Control Is Nothing)) Then container.Controls.Add(Me.Control.Clone)
  End Sub
End Class

My problem is that the ItemCommand event works randomly.

Private Sub DataGrid_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
  Select Case e.CommandName.ToUpper
    Case "Edit".ToUpper 
      'do something
    Case "Cancel".ToUpper 
      'do something
    Case "Update".ToUpper 
      'do something
    Case "Delete".ToUpper 
      'do something
    Case Else
  End Select
End Sub

If I push the Edit or Delete button it works fine. With the Edit button one row is set in Edit mode and if I push there a button a strange behaviour occur. The both Buttons Apply and Cancel are available. If I push them at the Apply button the Delete button will be used and if I push the Cancel button the ItemCommand event is not fired but the row is not anymore in edit mode.

Where is my mistake? What do i wrong, that this doesn't work correctly? If I would create the columns in markup it works fine. But not in my case with all on server side.

Thanks for any response.

mburm
  • 1,417
  • 2
  • 17
  • 37

1 Answers1

0

I thing u can call bindGrid() method after check page.ispostback on page_load like this :

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            return;
        }
       BindGrid();
    }

check this post.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71