Dynamic EditCommandColumn events

  • Thread starter Bruce E. Bonsall
  • Start date
B

Bruce E. Bonsall

I have a page that creates multiple datagrids at runtime. I have no problem setting the object's properties and assigning the event handlers. The 'Edit' linkbutton sucessfully puts the appropriate record in edit mode, but the CancelCommand and UpdateCommand do not fire their assigned handlers.

The 'Cancel' button ignores its assigned handler, and the 'Update' button fires the 'EditCommand' event.

I've seen this problem posted in numerous places, but without any explanations.

Can someone please provide enlightenment?

Thanks in advance!

-B

From http://www.developmentnow.com/g/12_0_0_0_0_0/dotnet-framework-aspnet-datagridcontrol.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 
E

Elton W

Hi Bruce,

What is the datagrid's viewstate, true or false? If it's
false, the datagrid can't work properly. If it's true, we
need to look for other reason to cause the problem.

HTH

Elton Wang
(e-mail address removed)
-----Original Message-----
I have a page that creates multiple datagrids at
runtime. I have no problem setting the object's
properties and assigning the event handlers. The 'Edit'
linkbutton sucessfully puts the appropriate record in edit
mode, but the CancelCommand and UpdateCommand do not fire
their assigned handlers.
The 'Cancel' button ignores its assigned handler, and
the 'Update' button fires the 'EditCommand' event.
 
B

Bruce Bonsall

EnableViewState=true

I have added another button column to each grid and have assigned a
deleteCommand handler that is working properly.

To suumarize:

All controls are being created at runtime, therefore all data is bound
with each pageload.

The EditCommand and DeleteCommand event handler are working properly.

The 'Update' link button triggers the EditCommand handler.

The 'Cancel' command does cancel out of edit mode, but not by using the
handler I've assigned it.

Thanks in advance for your help!
 
B

Bruce Bonsall

Here's the relevant code. Note that errorHandling and non-relevant
function calls have been removed for (hopefully) easier reading

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
paintPage()
End Sub

Private Sub paintPage()
' removed code gets the number of grids that need to be created

' get rid of existing controls
tblMain.Rows.Clear()

drGet = cmdGet.ExecuteReader
While drGet.Read
' create grid
Dim dg As New DataGrid

' set datagrid attributes, including event handlers
assignDGProperties(dg, drGet("staffHeading"))

' fill w/ data
dg.DataSource = fillGrid(drGet("staffHeading"))
dg.DataBind()

' create new table row
Dim tr As New TableRow
Dim tc As New TableCell
Dim lbl As New Label
lbl.Text = "<b><u>" & drGet("staffHeading") & "</u></b><br>"

'add controls to dynamic table
tc.Controls.Add(lbl)
tc.Controls.Add(dg)
tr.Cells.Add(tc)
tblMain.Rows.Add(tr)
End While
End Sub

Private Sub assignDGProperties(ByVal dg As DataGrid, ByVal heading As
String)
AddHandler dg.ItemCreated, AddressOf dg_ItemCreated

Dim ecc As New EditCommandColumn
With ecc
.ButtonType = ButtonColumnType.LinkButton
.CancelText = "Cancel"
.EditText = "Edit"
.UpdateText = "Update"
End With
dg.Columns.AddAt(0, ecc)

AddHandler dg.EditCommand, AddressOf dg_Edit
AddHandler dg.CancelCommand, AddressOf dg_Cancel
AddHandler dg.UpdateCommand, AddressOf dg_Update

Dim d As New ButtonColumn
With d
.CommandName = "Delete"
.Text = "Del"
End With

dg.Columns.AddAt(1, d)
AddHandler dg.DeleteCommand, AddressOf dg_Delete

' code removed from here adds bound and template columns

' format grid
With dg
.Font.Size = FontUnit.Point(8)
.ID = "dg" & heading
.Width = Unit.Percentage(100)
.AllowPaging = False
.HeaderStyle.Font.Size = New FontUnit(New Unit(8, UnitType.Point))
.HeaderStyle.Font.Name = "Arial"
.HeaderStyle.Font.Bold = True
.HeaderStyle.ForeColor.FromArgb(11, 61, 145)
.HeaderStyle.BackColor.FromArgb(238, 198, 129)
.AutoGenerateColumns = False
.EnableViewState = True
End With

End Sub

Private Function fillGrid(ByVal heading As String)
' build datasource and pass back to datagrid
End Function

Private Sub dg_ItemCreated(ByVal sender As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
' code here sizes columns and controls if listitemtype=edititem
End Sub

Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
' called when edit link in clicked in column(0)
CType(sender, DataGrid).EditItemIndex = e.Item.ItemIndex
CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
CType(sender, DataGrid).DataBind()
End Sub

Sub dg_Cancel(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
' called when cancel link in clicked in column(0)
CType(sender, DataGrid).EditItemIndex = -1
CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
CType(sender, DataGrid).DataBind()
End Sub

Sub dg_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
' called when cancel link in clicked in column(0)
CType(sender, DataGrid).EditItemIndex = -1
CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
CType(sender, DataGrid).DataBind()
End Sub

Sub dg_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'code here posts changes to database
End Sub
 
B

Bruce Bonsall

After days of grueling search and about a dozen posts to various
websites, I found a solution to this issue.

Because the controls had to be recreated on every pageload, the
editIndex of the grid in question got killed.

To correct:

On the initial pageload, I added the following code:
ViewState("grid_edit_index") = = -1

In the editCommand event handler, I added:
ViewState("grid_edit_index") = e.Item.ItemIndex

In the Cancel, Update, and Delete event handlers, I added
ViewState("grid_edit_index") = -1

In the sub where I set datagrid properties, I added:
dg.EditItemIndex = ViewState("grid_edit_index")

Badabing, badaboom.

Credit goes to
http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-datagrid/2582/5th-Re
-Post-with-NO-RESPONSE-FROM-MS
 
C

cj

That's wierd.. I think I did what you said, but now my EDIT button is doing
the UPDATE feature (but CANCEL and DELETE work)...!

Will keep looking at it...

cj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top