Search code examples
vb.netteleriktelerik-grid

How to fill RadGrid NestedViewTemplate from code behind?


Got a situation when I need to move RadGrid from front page to code.

It used to look like this:

<telerik:RadGrid id="rgBooks" runat="server"
    Height="300px"
    Skin="Office2007"     
    EnableViewState="true"
    AutoGenerateColumns="False" >
        <MasterTableView 
            EditMode="inplace"
            TableLayout="auto" 
            ClientDataKeyNames="BookID, BookName, Created, Modified, BookTypeName, BookTypeID"
            NoMasterRecordsText="No available books." >
            <HeaderStyle Font-Bold="true" Font-Size="Small" />
            <ItemStyle Font-Size="Small" Font-Names="Arial" HorizontalAlign="Left" Wrap="True" BackColor="White"/>
            <HeaderStyle Wrap="False" Width="99%" HorizontalAlign="Left"/>
            <AlternatingItemStyle Font-Size="Small" Font-Names="Arial" HorizontalAlign="Left" Wrap="True" BackColor="White"/>  
            <NestedViewTemplate>
                <table>
                    <tr>
                        <td>
                            BookType Type:
                        </td>
                        <td>
                            <asp:DropDownList runat="server" ID="ddlBookTypes" AutoPostBack="true" /> 
                        </td>
                    </tr>

                    ....
                    ....

            </NestedViewTemplate>
            <columns>
                <telerik:GridBoundColumn HeaderText="Book Name" HeaderStyle-Width="33%" ItemStyle-Width="33%" UniqueName="BookName" DataField="BookName" />
                <telerik:GridBoundColumn HeaderText="Created" HeaderStyle-Width="33%" ItemStyle-Width="33%" UniqueName="Created" DataField="Created" />
                <telerik:GridBoundColumn HeaderText="Modified" HeaderStyle-Width="33%" ItemStyle-Width="33%" UniqueName="Modified" DataField="Modified" />
            </columns>

            ....
            ....

</telerik:RadGrid> 

And recreating this in code looks like this:

Dim rgBooks As New RadGrid : With rgBooks
   .ID = "rgBooks"
   .Height = New Unit(300, UnitType.Pixel)
   .Skin = "Office2007"

   ....
   ....       

End With

Me.placeHolder.Controls.Add(rgBooks)

It works fine EXCEPT for <NestedViewTemplate>. Didn't found a way to send there my values (table), and literally no documentation about how to do it. As I understand, it supposed to look like rgBooks.MasterTableView.NestedViewTemplate.InstantiateIn( and here my data ) but each attempt to sent there something ended with failure, and with exception Object reference not set to an instance of an object.

Also tried to sent as LiteralControl.

rgBooks.MasterTableView.Controls.Add(New LiteralControl("<NestedViewTemplate>"))
rgBooks.MasterTableView.Controls.Add(New LiteralControl("<table>"))
rgBooks.MasterTableView.Controls.Add(New LiteralControl("<tr>"))
....
....

Didn't worked out.

How I suppose to fill NestedViewTemplate in code correctly (it is ITemplate by the way)?


Solution

  • Ok, figured out how it supposed to work.

    A custom class required

    Public Class NestedTemplate
        Implements ITemplate
    
        Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
    
            container.Controls.Add(New LiteralControl("<table>"))
            container.Controls.Add(New LiteralControl("<tr>"))
    
            ....
            ....
    
            container.Controls.Add(New LiteralControl("</table>"))
        End Sub
    End Class
    

    And then just

    rgBooks.MasterTableView.NestedViewTemplate = new NestedTemplate
    

    Oh boy, how much time I wasted on this thing...