0

我仅以编程方式使用 DataGrid。

 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

Page_Load我绑定DataSource并添加列。

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

我的问题是该ItemCommand事件随机进行。

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

如果我按下编辑或删除按钮,它工作正常。使用“编辑”按钮将一行设置为“编辑”模式,如果我按下一个按钮,就会发生奇怪的行为。按钮应用和取消都可用。如果我在应用按钮上按下它们,将使用删除按钮,如果我按下取消按钮,ItemCommand则不会触发事件,但该行不再处于编辑模式。

我的错误在哪里?我错了什么,这不能正常工作?如果我要在标记中创建列,它可以正常工作。但在我的情况下,所有服务器端都不是。

感谢您的任何回复。

4

1 回答 1

0

我觉得你可以在这样bindGrid()检查后调用方法:page.ispostbackpage_load

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

检查这篇文章

于 2014-01-21T05:55:58.550 回答