EditCommand (Datagrid nested in a Data Repeater)

R

reflektmedia

I have a Datarepeater (parent) who has a datagrid(child) for each item
of the repeater. I have the Add/Insert, and Delete working correctly
in the datagrid. The problem occurs when clicking EDIT in the
datagrid(child).

I can debug and see that it is correctly going into my EditCommand
event (see below) and the EditItemIndex is being set. However as soon
as the data is rebound/refreshed the datarepeater(parent) is refreshed
and the EditItemIndex is reset back to -1.

---------------------------------------------------------------------------
Protected Sub dgChild_EditCommand(ByVal source As Object, ByVal
e As System.Web.UI.WebControls.DataGridCommandEventArgs)

Dim myInnerDG As DataGrid = CType(source, DataGrid)
myInnerDG.EditItemIndex = e.Item.ItemIndex

RefreshData()

End Sub
 
A

Ahmed Fouad

As of my understanding, rebinding the parent repeater causes any nested
controls (including nested DataGrid) to reinstantiate. This is the reason
why the EditItemIndex property of the child grid resets to -1 (the default).
You need to store the EditItemIndex in a class level variable and then use
this variable in the ItemCreated event of the Repeater (parent) control.
Something like the following should do the trick:

' declare class level variables to preserve nested state
Private m_childGridIndex As Integer ' allows us to locate instance of
the child grid for which EditCommand was last fired
Private m_editItemIndex As Integer ' you already know whats this
for....

Protected Sub dgChild_EditCommand(. . .)
Dim myInnerDG As DataGrid = CType(source, DataGrid)


' no need to do that because it will be reset to -1 after invoking
RefreshData() method
'myInnerDG.EditItemIndex = e.Item.ItemIndex

' The following lines will rescue our precious indices to be used in
Repeater's ItemCreated event handler...
m_childGridIndex = CType(myInnerDG.NamingContainer,
RepeaterItem).ItemIndex
m_editItemIndex = e.Item.ItemIndex

RefreshData()
End Sub

Private Sub Repeater_ItemCreated(. . .)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
If e.Item.ItemIndex = m_childGridIndex Then
Dim dgChild As DataGrid =
DirectCast(e.Item.FindControl("dgChild"), DataGrid)
dgChild.EditItemIndex = m_editItemIndex
End If
End If
End Sub
 
R

reflektmedia

Ahmed, Thank you very much for your tips

I got it working after your suggestions.

I was headed down the same path before I got your suggestion, I errored
on these topics:
1) I was trying to set the EditItemIndex in the Repeaters_ItemDataBound
instead of Repeaters_ItemCreated.
2) I was trying to check to see which datagrid to edit by storing the
dg.ClientID instead of the ChildGridIndex. This worked fine while in
_ItemDataBound but once it was moved to _ItemCreated the ClientID was
not unique.

I really appreciate the time you took to answer this question. I have
seen a lot of other old posts with the same question but no answer.
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top