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

Problem accessing controls in NoRecordsTemplate

3 Answers 38 Views
Grid
This is a migrated thread and some comments may be shown as answers.
andieje
Top achievements
Rank 1
andieje asked on 13 Aug 2013, 05:43 PM
Hello

I am using an old version of RadGrid (2008 Q3) but I hope you will still be able to help.

I am trying to access the controls in my NoRecordsTemplate in the code behind but the code I have written is not fired. I am trying to access the template in the ItemCreated event

Protected Sub gridJourneys_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gridJourneys.ItemCreated
     If TypeOf e.Item Is GridNoRecordsItem Then
 
         Dim norecordItem As GridNoRecordsItem = CType(gridJourneys.MasterTableView.GetItems(GridItemType.NoRecordsItem)(0), GridNoRecordsItem)
 
         Dim l As Label = norecordItem.FindControl("lblNoJourneys")
         l.Text = "hello"
         Throw New Exception("here")
     End If
 
 
 
 End Sub

Here is the template

<NoRecordsTemplate>
There are no journeys on this date.
         <asp:Label runat="server" ID="lblNoJourneys" />
     
</NoRecordsTemplate>

The label text is not set and the exception isn't thrown demonstrating that this code is not reached

Thanks in advance for any help you can provide

3 Answers, 1 is accepted

Sort by
0
A2H
Top achievements
Rank 1
answered on 14 Aug 2013, 01:59 AM

Hi,

If you are assigning a datasource to the grid in codebehind, then if there are no records in datasource(datatable) you need to bind the RadGrid to an empty collection.

Please see the code which I tired and it works fine at my end.

Protected Sub gridJourneys_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
  
        If TypeOf e.Item Is GridNoRecordsItem Then
  
            Dim norecordItem As GridNoRecordsItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.NoRecordsItem)(0), GridNoRecordsItem)
  
            Dim l As Label = norecordItem.FindControl("lblNoJourneys")
            l.Text = "hello"
        End If
    End Sub

    

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
      Handles Me.Load
        If Not IsPostBack Then
  
            Dim table As New DataTable()
            table = GetTable()
            If (table.Rows.Count > 0) Then
                RadGrid1.DataSource = table
            Else
                RadGrid1.DataSource = New Object() {}
            End If
            RadGrid1.DataBind()
        End If
    End Sub

    

Private Shared Function GetTable() As DataTable
        
        Dim table As New DataTable()
        table.Columns.Add("Test1", GetType(String))
        table.Columns.Add("Test2", GetType([Decimal]))
        table.Columns.Add("Test3", GetType(String))
        table.Columns.Add("Test4", GetType(String))
        table.Columns.Add("Test5", GetType(String))
        table.Columns.Add("Test6", GetType(DateTime))
     
        'table.Rows.Add("Test1", 1, "Test1", "Test1", "Test1", DateTime.Now)
        'table.Rows.Add("Test2", 2, "Test1", "Test1", "Test1", DateTime.Now.AddDays(1))
        'table.Rows.Add("Test3", 3, "Test1", "Test1", "Test1", DateTime.Now.AddDays(2))
        'table.Rows.Add("Test4", 4, "Test1", "Test1", "Test1", DateTime.Now.AddDays(3))
        'table.Rows.Add("Test5", 5, "Test1", "Test1", "Test1", DateTime.Now.AddDays(4))
        'table.Rows.Add("Test6", 6, "Test1", "Test1", "Test1", DateTime.Now.AddDays(4))
        Return table
    End Function

Thanks,
A2H
0
Accepted
Princy
Top achievements
Rank 1
answered on 14 Aug 2013, 06:01 AM
Hi,

To access the NoRecordsTemplate you can try the following code snippet.Make sure that you have not set ShowHeadersWhenNoRecords="false" and the grid control should be bound to an empty collection instead of null/Nothing.
The code works fine at my end,the version is 2013.2.611.40.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemCreated="RadGrid1_ItemCreated">
        <MasterTableView ShowHeadersWhenNoRecords="true">
             <NoRecordsTemplate>
                 <div>
                 There are no records to display
                 </div>
                 <asp:Label runat="server" ID="label1" />
             </NoRecordsTemplate>
         </MasterTableView>
  </telerik:RadGrid>

VB:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
    If TypeOf e.Item Is GridNoRecordsItem Then
        Dim norecordItem As GridNoRecordsItem = DirectCast(e.Item, GridNoRecordsItem)
        Dim l As Label = DirectCast(norecordItem.FindControl("label1"), Label)
        l.Text = "Your Text"
    End If
End Sub

Thanks,
Princy
0
andieje
Top achievements
Rank 1
answered on 30 Aug 2013, 02:13 PM
Thanks - i had set the ShowNoRecords = false
Tags
Grid
Asked by
andieje
Top achievements
Rank 1
Answers by
A2H
Top achievements
Rank 1
Princy
Top achievements
Rank 1
andieje
Top achievements
Rank 1
Share this question
or