This is a migrated thread and some comments may be shown as answers.

Changing nested FormView.FormViewMode on all rows except the clicked one

1 Answer 71 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Massimiliano
Top achievements
Rank 1
Massimiliano asked on 08 Nov 2013, 05:08 PM
What I'm trying to accomplish is quite simple but I still didn't grasp all the ways of iterating the grid items in all the events.
I have a grid with a custom FormView inside <NestedViewTemplate> (well the FormView really is inside a user control in <NestedViewTemplate>).

What I would like to happen in the gui is this: when you click on the edit button (wich triggers a custom edit logic) I would like to check all OTHER rows to see if:
1) they are expanded (and in this case collapse them so that only the active edited row is expanded)
2) set the FormView.FormViewMode of each row OTHER than the edited one to ReadOnly so that if the user opens the row again in a later moment he finds it in view mode and no more in edit mode.
3) set the <asp:Panel runat="server" ID="RadGrid1DetailContainer" CssClass="RadGridDetailContainer" Visible="true">, wich is the first item in the <NestedViewTemplate> to visible = false for all the row OTHER than the edited one

The expand part is easy, and the custom logic wich expands the edited row and sets the FormView in edit mode is easy but I cannot find a way to address points 2 and 3.

Here is my logic so far that triggers in RadGrid1_ItemCommand when user clicks on the edit button and throws the EditFormView command. 

ASPX part:

<NestedViewTemplate>
    <asp:Panel runat="server" ID="RadGrid1DetailContainer" CssClass="RadGridDetailContainer" Visible="true">
        <telerik:RadTabStrip runat="server" ID="RadGrid1DetailTabStrip" MultiPageID="RadGrid1DetailMultipage1" SelectedIndex="0" CssClass="RadGridTabStrip">
            <Tabs>
                <telerik:RadTab runat="server" Text="Dati utente" PageViewID="RadGrid1DetailRadPageView1">
                </telerik:RadTab>
                <telerik:RadTab runat="server" Text="Anagrafica" PageViewID="RadGrid1DetailRadPageView2">
                </telerik:RadTab>
                <telerik:RadTab runat="server" Text="Altri dati" PageViewID="RadGrid1DetailRadPageView3">
                </telerik:RadTab>
            </Tabs>
        </telerik:RadTabStrip>
        <telerik:RadMultiPage runat="server" ID="RadGrid1DetailMultipage1" SelectedIndex="0" RenderSelectedPageOnly="false">
            <telerik:RadPageView runat="server" ID="RadGrid1DetailRadPageView1" CssClass="RadGridPageView">
                <asp:FormView id="RadGrid1DetailForm1" CssClass="RadGridDetailForm" runat="server">
                    <ItemTemplate>
                        <div class="form-horizontal form-stripe" style="padding: 12px;">
                            <eva:FormUserDetail ID="FormUserDetail1" Item="<%# Container.DataItem %>" View="Detail1" runat="server" />
                            <div class="form-actions">
                               <asp:LinkButton ID="FormConfirmUpdate" runat="server" CssClass="btn btn-primary" CommandName="EditFormView">
                                    <i class="icon icon-pencil"></i> Edit
                                </asp:LinkButton>
                           </div>
                        </div>
                    </ItemTemplate>
 
                    <EditItemTemplate>
                        <div class="form-horizontal form-stripe" style="padding: 12px;">
                            <eva:FormUserEdit ID="FormUserDetail2" Item="<%# Container.DataItem %>" View="Detail1" runat="server" />
                            <div class="form-actions">
                              <asp:Button runat="server" ID="FormConfirmUpdate" CssClass="btn btn-success" Text="Submit" CommandName="FormConfirmUpdate" />
                               <asp:Button runat="server" ID="FormCancelUpdate" CssClass="btn" Text="Cancel" CommandName="FormCancelUpdate" />
                            </div>
                        </div>
                    </EditItemTemplate>
                </asp:FormView>
            </telerik:RadPageView>
 
            <telerik:RadPageView runat="server" ID="RadGrid1DetailRadPageView2">
 
            </telerik:RadPageView>
            <telerik:RadPageView runat="server" ID="RadGrid1DetailRadPageView3">
 
            </telerik:RadPageView>
        </telerik:RadMultiPage>
    </asp:Panel>
</NestedViewTemplate>

The code behind for the EditFormView command is:

Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand
    Dim gridFormView As FormView
    Dim primaryKey As Integer
  
    If TypeOf (e.Item) Is GridDataItem Then
        gridFormView = DirectCast(e.Item, GridDataItem).ChildItem.FindControl("RadGrid1DetailContainer").FindControl("RadGrid1DetailMultipage1").FindControl("RadGrid1DetailRadPageView1").FindControl("RadGrid1DetailForm1")
        primaryKey = Convert.ToInt32(DirectCast(e.Item, GridDataItem).GetDataKeyValue("UserId"))
    End If
  
    Select Case e.CommandName
        Case "EditFormView"
            e.Item.Expanded = True
  
            If TypeOf (e.Item) Is GridDataItem Then
                DirectCast(e.Item, GridDataItem).ChildItem.FindControl("RadGrid1DetailContainer").Visible = True
  
                ' Collapse all other opened details
                For Each item As GridItem In e.Item.OwnerTableView.Items
                    If item.Expanded AndAlso Not item Is e.Item Then
                        item.Expanded = False
                        DirectCast(item.DataItem, GridDataItem).ChildItem.FindControl("RadGrid1DetailContainer").Visible = False
  
                        Dim gridFormViewToBeClosed As FormView = DirectCast(item.DataItem, GridDataItem).ChildItem.FindControl("RadGrid1DetailContainer").FindControl("RadGrid1DetailMultipage1").FindControl("RadGrid1DetailRadPageView1").FindControl("RadGrid1DetailForm1")
  
                        ' Put form always in ReadOnly when is expanded
                        If gridFormViewToBeClosed.CurrentMode <> FormViewMode.ReadOnly Then
                            gridFormViewToBeClosed.ChangeMode(FormViewMode.ReadOnly)
                        End If
                    End If
                Next item
            End If
  
            ' Put form in EditMode when is expanded
            If gridFormView.CurrentMode <> FormViewMode.Edit Then
                gridFormView.ChangeMode(FormViewMode.Edit)
            End If
  
            ' Fill the form view from the db
            SelectUserEdit(primaryKey, gridFormView)
    End Select
End Sub

The item.Expanded = False part works like expected. The issue I'm facing is how to retrieve a reference to the FormView(s) and to the RadGrid1DetailContainer  for each other row

I tried with 
DirectCast(item.DataItem, GridDataItem).ChildItem.FindControl("RadGrid1DetailContainer").Visible = False
But I guess item.DataItem is not what I'm looking for since I always have a  System.NullReferenceException on that line... 

Any hint on how to iterate and access the FormView in all the rows other than the one where the click command triggered, would be greatly appreciated. 

Thanks in advance




1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 13 Nov 2013, 04:59 PM
Hello,

 A suitable event which will allow you to iterate over all the items in the grid is the ItemCreated event, which is fired once for each item in the control. There you can use similar logic to the one the ItemCommand event to access the inner RadGrid1DetailContainer and from there find the FormView. Here a sample code that you can use as a starting point:

Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
        'will be executed for each item in the grid
         If TypeOf e.Item Is GridNestedViewItem Then
            Dim detailContainer = e.Item.FindControl("RadGrid1DetailContainer")
            detailContainer.Visible = False
            Dim gridFormView = detailContainer.FindControl("RadGrid1DetailMultipage1").FindControl("RadGrid1DetailRadPageView1").FindControl("RadGrid1DetailForm1")
        End If
    End Sub

Additional information and code samples can find controls and iterating over the grid items can also be found in the following help topic:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html
http://www.telerik.com/help/aspnet-ajax/grid-nestedviewtemplate.html

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Massimiliano
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or