我只是以编程方式使用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
如果我按下编辑或删除按钮,它可以正常工作。使用“编辑”按钮,在编辑模式下设置一行,如果我按下按钮,则会出现奇怪的行为。可以使用“应用”和“取消”按钮。如果我在Apply按钮上按下它们,将使用Delete按钮,如果我按下Cancel按钮,ItemCommand
事件不会被触发,但该行不再处于编辑模式。
我的错误在哪里?我怎么了,这不能正常工作?如果我在标记中创建列,它可以正常工作。但在我的情况下并不是所有服务器端。
感谢您的回复。
答案 0 :(得分:0)
我可以在bindGrid()
上page.ispostback
之后调用page_load
方法,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
return;
}
BindGrid();
}
检查this post。