Question

I have a hierarchical Telerik RadGrid that sometimes contains child entries that are empty. For these, I want to overwrite the default text "No child records to display" with something user locale specific.

So I can do this:

    <telerik:RadGrid ID ="SettingsGrid" ... />
        <mastertableview ... />
            <DetailTables>
                <telerik:GridTableView ... />
                    <asp:Label ID="NoRecordLabel" runat="server" Text="whatever"/></div></NoRecordsTemplate>

Which causes the text "whatever" to appear when it should.

But I obviously want to do this dynamically, but I have failed in both of two ways:

1) By referencing my .resx file in the .ascx file. I import it's namespace and reference a certain resource as such:

Text="<%$ Resx:SiteTextResources.Globals_Close %>"

(This works in other files in the same solution)

But this only produces empty text.

2) I haven't been successful at retrieving the Label programatically from the code behind. I looked at this: http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-access-controls-in-norecordstemplate.aspx but didn't get that approach to work, as I just can't seem to find the Label. I get an OutOfBoundsException, which I guess means the GetItems() method returns null.

Any ideas? Would appreciate it a lot!

Was it helpful?

Solution

Please try with the below code snippet. Let me know if any concern.

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
    OnItemDataBound="RadGrid1_ItemDataBound" 
    OnDetailTableDataBind="RadGrid1_DetailTableDataBind" 
    onprerender="RadGrid1_PreRender">
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn>
            </telerik:GridEditCommandColumn>
        </Columns>
        <DetailTables>
            <telerik:GridTableView Name="Child">
                <NoRecordsTemplate>
                    <asp:Label ID="NoRecordLabel" runat="server" Text="whatever" />
                </NoRecordsTemplate>
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

ASPX.CS

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data1 = new[] {
           new { ID = 1, Name ="Name_1",FileName = "jayesh.jpg"},
           new { ID = 2, Name = "Name_2",FileName = "jayesh.jpg"},
           new { ID = 3, Name = "Name_3",FileName = "jayesh.jpg"},
           new { ID = 4, Name = "Name_4",FileName = "jayesh.jpg"},
           new { ID = 5, Name = "Name_5",FileName = "jayesh.jpg"}
       };

    RadGrid1.DataSource = data1;
}

protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
    e.DetailTableView.DataSource = new Object[0];
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if (item.HasChildItems && item.Expanded)
        {
            if (item.ChildItem.NestedTableViews[0].GetItems(GridItemType.NoRecordsItem).Count() > 0)
            {
                GridNoRecordsItem norecordItem = (GridNoRecordsItem)item.ChildItem.NestedTableViews[0].GetItems(GridItemType.NoRecordsItem)[0];
                Label NoRecordLabel = (Label)norecordItem.FindControl("NoRecordLabel");
                NoRecordLabel.Text = DateTime.Now.ToString();
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top