W
wannensn
Hello,
I need to create a DataGrid at runtime with a EditCommandColumn. Everything
works fine only the events of the EditCommandColumn, which are not firing
correctly. If I click on "Edit" the right Event-Handler is used but when I
click on "Update" the Event-Handler for the EditCommand is used, clicking on
"Cancel" no Event-Handler is used;-(
I read several articles and posts about this, but I never found a solution
that is working.
I hope someone can help me.
Thanks,
Stephan
Here is my code:
===> WebForm1.aspx:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"
debug="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
==> Then code behind WebForm1.aspx.vb:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Vom Web Form Designer generierter Code "
'Dieser Aufruf ist für den Web Form-Designer erforderlich.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
' Added by hand for access to the form.
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
'HINWEIS: Die folgende Platzhalterdeklaration ist für den Web Form-Designer
erforderlich.
'Nicht löschen oder verschieben.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: Dieser Methodenaufruf ist für den Web Form-Designer
erforderlich
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
BindGrid()
End Sub
Private Sub BindGrid()
'generate some test data
Dim dTable As New Data.DataTable
Dim dCol1 As New Data.DataColumn("Column1")
Dim dCol2 As New Data.DataColumn("Column2")
dTable.Columns.Add(dCol1)
dTable.Columns.Add(dCol2)
Dim dRow As Data.DataRow = dTable.NewRow
dRow.Item("Column1") = "Value1.1"
dRow.Item("Column2") = "Value1.2"
dTable.Rows.Add(dRow)
dRow = dTable.NewRow
dRow.Item("Column1") = "Value2.1"
dRow.Item("Column2") = "Value2.2"
dTable.Rows.Add(dRow)
Dim grid As New DataGrid
grid.EnableViewState = True
grid.AutoGenerateColumns = False
Dim editCol As New EditCommandColumn
editCol.EditText = "Edit"
editCol.CancelText = "Cancel"
editCol.UpdateText = "Update"
editCol.ButtonType = ButtonColumnType.LinkButton
grid.Columns.Add(editCol)
Dim gridCol1 As New BoundColumn
gridCol1.DataField = "Column1"
gridCol1.HeaderText = "Column1"
gridCol1.ReadOnly = True
grid.Columns.Add(gridCol1)
Dim gridCol2 As New BoundColumn
gridCol2.DataField = "Column2"
gridCol2.HeaderText = "Column2"
grid.Columns.Add(gridCol2)
grid.DataSource = dTable
grid.DataBind()
AddHandler grid.EditCommand, AddressOf grid_EditCommand
AddHandler grid.CancelCommand, AddressOf grid_CancelCommand
AddHandler grid.UpdateCommand, AddressOf grid_UpdateCommand
Form1.Controls.Clear()
Form1.Controls.Add(grid)
Session("Recordset") = dTable
End Sub
Public Sub grid_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
'set the index of the row to be edited
Dim grid As DataGrid = CType(source, DataGrid)
grid.EditItemIndex = CInt(e.Item.ItemIndex)
'rebind grid
grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
grid.DataBind()
End Sub
Public Sub grid_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
'make sure that no row is marked for editing
Dim grid As DataGrid = CType(source, DataGrid)
grid.EditItemIndex = -1
'rebind grid
grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
grid.DataBind()
End Sub
Public Sub grid_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) 'Handles
grid.UpdateCommand
Dim grid As DataGrid = CType(source, DataGrid)
Dim dTable As Data.DataTable
dTable = CType(Session("Recordset"), DataTable)
Dim NewVal As String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
dTable.Rows(grid.EditItemIndex).Item(1) = NewVal
dTable.AcceptChanges()
grid.EditItemIndex = -1
'rebind grid
grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
grid.DataBind()
End Sub
End Class
I need to create a DataGrid at runtime with a EditCommandColumn. Everything
works fine only the events of the EditCommandColumn, which are not firing
correctly. If I click on "Edit" the right Event-Handler is used but when I
click on "Update" the Event-Handler for the EditCommand is used, clicking on
"Cancel" no Event-Handler is used;-(
I read several articles and posts about this, but I never found a solution
that is working.
I hope someone can help me.
Thanks,
Stephan
Here is my code:
===> WebForm1.aspx:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"
debug="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
==> Then code behind WebForm1.aspx.vb:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Vom Web Form Designer generierter Code "
'Dieser Aufruf ist für den Web Form-Designer erforderlich.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
' Added by hand for access to the form.
Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
'HINWEIS: Die folgende Platzhalterdeklaration ist für den Web Form-Designer
erforderlich.
'Nicht löschen oder verschieben.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: Dieser Methodenaufruf ist für den Web Form-Designer
erforderlich
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
BindGrid()
End Sub
Private Sub BindGrid()
'generate some test data
Dim dTable As New Data.DataTable
Dim dCol1 As New Data.DataColumn("Column1")
Dim dCol2 As New Data.DataColumn("Column2")
dTable.Columns.Add(dCol1)
dTable.Columns.Add(dCol2)
Dim dRow As Data.DataRow = dTable.NewRow
dRow.Item("Column1") = "Value1.1"
dRow.Item("Column2") = "Value1.2"
dTable.Rows.Add(dRow)
dRow = dTable.NewRow
dRow.Item("Column1") = "Value2.1"
dRow.Item("Column2") = "Value2.2"
dTable.Rows.Add(dRow)
Dim grid As New DataGrid
grid.EnableViewState = True
grid.AutoGenerateColumns = False
Dim editCol As New EditCommandColumn
editCol.EditText = "Edit"
editCol.CancelText = "Cancel"
editCol.UpdateText = "Update"
editCol.ButtonType = ButtonColumnType.LinkButton
grid.Columns.Add(editCol)
Dim gridCol1 As New BoundColumn
gridCol1.DataField = "Column1"
gridCol1.HeaderText = "Column1"
gridCol1.ReadOnly = True
grid.Columns.Add(gridCol1)
Dim gridCol2 As New BoundColumn
gridCol2.DataField = "Column2"
gridCol2.HeaderText = "Column2"
grid.Columns.Add(gridCol2)
grid.DataSource = dTable
grid.DataBind()
AddHandler grid.EditCommand, AddressOf grid_EditCommand
AddHandler grid.CancelCommand, AddressOf grid_CancelCommand
AddHandler grid.UpdateCommand, AddressOf grid_UpdateCommand
Form1.Controls.Clear()
Form1.Controls.Add(grid)
Session("Recordset") = dTable
End Sub
Public Sub grid_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
'set the index of the row to be edited
Dim grid As DataGrid = CType(source, DataGrid)
grid.EditItemIndex = CInt(e.Item.ItemIndex)
'rebind grid
grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
grid.DataBind()
End Sub
Public Sub grid_CancelCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
'make sure that no row is marked for editing
Dim grid As DataGrid = CType(source, DataGrid)
grid.EditItemIndex = -1
'rebind grid
grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
grid.DataBind()
End Sub
Public Sub grid_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) 'Handles
grid.UpdateCommand
Dim grid As DataGrid = CType(source, DataGrid)
Dim dTable As Data.DataTable
dTable = CType(Session("Recordset"), DataTable)
Dim NewVal As String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
dTable.Rows(grid.EditItemIndex).Item(1) = NewVal
dTable.AcceptChanges()
grid.EditItemIndex = -1
'rebind grid
grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
grid.DataBind()
End Sub
End Class