Imports Telerik.Web.UI

Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub RadAjaxManager1_AjaxRequest(ByVal sender As Object, ByVal e As Telerik.Web.UI.AjaxRequestEventArgs) Handles RadAjaxManager1.AjaxRequest
        Dim postBackData As String() = e.Argument.Split("|")
        Select Case postBackData(0)
            Case "Expand"
                Dim index As Integer = postBackData(2).LastIndexOf("_")
                Dim tableIndex As Integer = Integer.Parse(postBackData(2).Substring(index + 1))

                Dim item As GridDataItem = GetTableView(Me.RadGrid1.MasterTableView, postBackData(1)).Items(tableIndex)
                If (item.Expanded) Then
                    item.Expanded = False
                    item.Selected = True
                Else
                    item.Expanded = True
                    item.Selected = True
                End If
        End Select
    End Sub
    Private Function GetTableView(ByVal currTableView As GridTableView, ByVal controlId As String) As GridTableView
        If currTableView.ClientID = controlId Then
            Return currTableView
        Else
            Dim item As GridItem
            For Each item In currTableView.Items
                If item.HasChildItems And TypeOf item Is GridDataItem Then
                    Dim nestedView As GridTableView
                    For Each nestedView In CType(item, GridDataItem).ChildItem.NestedTableViews()
                        Dim res As GridTableView = GetTableView(nestedView, controlId)
                        If Not res Is Nothing Then
                            Return res
                        End If
                    Next nestedView
                End If
            Next item
        End If
    End Function
End Class
