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

Create a FilterTemplate In Code-Behinde

2 Answers 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 18 Nov 2014, 09:05 AM
Hello,

I am working on a project with RadGrid.

I am trying to create a FilterTemplate like this video:
http://youtu.be/jOY3NFzdoVY

This is FilterTemplate’s code, created in the designer:
<FilterTemplate>
                            <telerik:RadMenu ID="RadMenu1" runat="server" OnClientItemOpened="itemOpened">
                                <Items>
                                    <telerik:RadMenuItem PostBack="false" Text="Date Filter">
                                        <ContentTemplate>
                                            <div id="DateFilterDivWrapper" class="Wrapper">
                                                <table id="DateFilterTable">
                                                    <tr>
                                                        <td class="CustomFilter">
                                                            <asp:Label ID="lbl_From" runat="server" Text="From"></asp:Label>
                                                        </td>
                                                        <td class="CustomFilter">
                                                            <telerik:RadDateTimePicker ID="dt_picker_From" runat="server"></telerik:RadDateTimePicker>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td class="CustomFilter">
                                                            <asp:Label ID="lbl_To" runat="server" Text="To"></asp:Label>
                                                        </td>
                                                        <td class="CustomFilter">
                                                            <telerik:RadDateTimePicker ID="dt_picker_To" runat="server"></telerik:RadDateTimePicker>
  
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td colspan="2" style="text-align: center;" class="CustomFilter">
                                                            <telerik:RadButton ID="btn_Filter_Date" runat="server" Text="Filter"></telerik:RadButton>
                                                        </td>
                                                    </tr>
                                                </table>
  
  
                                            </div>
                                        </ContentTemplate>
                                    </telerik:RadMenuItem>
                                </Items>
                            </telerik:RadMenu>
  
                        </FilterTemplate>

However, I want to code to be code-behind, like this:
01.Private Sub RadGrid1_ColumnCreated(sender As Object, e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles RadGrid1.ColumnCreated
02.        If e.Column.ColumnType = "GridDateTimeColumn" Then
03.            Dim xx As New MyCustomFilteringColumn
04.            e.Column.FilterTemplate = xx
05.        End If
06.    End Sub
07. 
08.Public Class MyCustomFilteringColumn
09.    Implements ITemplate
10.  
11.    Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
12.        Dim RadMenuItem_DivDate As New RadMenuItem
13.        Dim insidMenuControl As New Control
14.  
15.  
16.        Dim lbl_From, lbl_To As New Label()
17.        Dim dt_picker_From, dt_picker_To As New RadDateTimePicker
18.        Dim btn As New RadButton()
19.        lbl_From.ID = "lbl_From"
20.        lbl_To.ID = "lbl_To"
21.        lbl_From.Text = "From" 'can take from resource  (all the .text)
22.        lbl_To.Text = "To"
23.        dt_picker_From.ID = "dt_picker_From"
24.        dt_picker_To.ID = "dt_picker_To"
25.        btn.ID = "btn_Filter"
26.        btn.Text = "Filter"
27.  
28.  
29.  
30.  
31.  
32.        insidMenuControl.Controls.Add(New Literal With {.Text = "<div id=""DateFilterDivWrapper"">"})
33.        insidMenuControl.Controls.Add(New Literal With {.Text = "<table id=""DateFilterTable""> <tr>  <td>"})
34.        insidMenuControl.Controls.Add(lbl_From)
35.        insidMenuControl.Controls.Add(New Literal With {.Text = "</td> <td>"})
36.        insidMenuControl.Controls.Add(dt_picker_From)
37.        insidMenuControl.Controls.Add(New Literal With {.Text = "</td>  </tr> <tr> <td>"})
38.        insidMenuControl.Controls.Add(lbl_To)
39.        insidMenuControl.Controls.Add(New Literal With {.Text = " </td> <td>"})
40.        insidMenuControl.Controls.Add(dt_picker_To)
41.        insidMenuControl.Controls.Add(New Literal With {.Text = "</td>  </tr> <tr>   <td colspan=""2"" style=""text-align:center;"">"})
42.        insidMenuControl.Controls.Add(btn)
43.        insidMenuControl.Controls.Add(New Literal With {.Text = "  </td> </tr>  </table>"})
44.        insidMenuControl.Controls.Add(New Literal With {.Text = "</div>"})
45.  
46.        RadMenuItem_DivDate.PostBack = False
47.        RadMenuItem_DivDate.Text = "Date Filter"
48.  
49.        RadMenuItem_DivDate.ContentTemplate.InstantiateIn(insidMenuControl)
50.  
51.        Dim RadMenu_DateFilter As New RadMenu
52.        RadMenu_DateFilter.ID = "RadMenu_DateFilter"
53.        RadMenu_DateFilter.OnClientItemOpened = "itemOpened"
54.        RadMenu_DateFilter.Items.Add(RadMenuItem_DivDate)
55.  
56.        container.Controls.Add(RadMenu_DateFilter)
57.  
58.    End Sub
59.End Class

However, when I try to transfer the above code to code-behind I receive the following exception (It occurs on line 49):
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I would be happy to get a code-behind solution.

Thank you,
Daniel.

2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 21 Nov 2014, 07:12 AM
Hello Daniel,

The issue that you are experiencing is due to the fact that the ContentTemplate of the RadMenuItem must be defined in the same manner as you are defining all other templates - by setting it to an instance of a class that implements ITemplate, something like the following:
Dim customContentTemplate As New MyCustomMenuItemContentTemplate
RadMenuItem_DivDate.ContentTemplate = customContentTemplate

I should also point out that if you need to change the structure of the grid programmatically you need to create the entire RadGrid in the code-behind, as documented in the following help article:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Daniel
Top achievements
Rank 1
answered on 23 Nov 2014, 10:30 AM
Hi Konstantin Dikov,

Thank you for your reply - it works!!

Thanks,
Daniel.
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or