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

Finding Label in hierarchical RadGrid

6 Answers 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
C
Top achievements
Rank 1
C asked on 11 Sep 2013, 06:47 AM
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" ... /><br>
            <mastertableview ... /><br>
                <DetailTables><br>
                    <telerik:GridTableView ... /><br>
                        <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!

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 11 Sep 2013, 08:41 AM
Hi ,

Please try the following code snippet to access the NoRecordsTemplate,in the code behind.

C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
       if (e.Item is GridNoRecordsItem)
       {
           GridNoRecordsItem norecordItem = (GridNoRecordsItem)e.Item;
           Label lbl = (Label)norecordItem.FindControl("NoRecordLabel");
           lbl.Text = "Your Text";
       }
   }

Thanks,
Princy
0
C
Top achievements
Rank 1
answered on 11 Sep 2013, 08:52 AM
Hi,

Unfortunately, through debugging I find no e.Item is ever a GridNoRecordsItem.
0
Princy
Top achievements
Rank 1
answered on 11 Sep 2013, 09:02 AM
Hi,

The code works fine at my end,here is the full code snippet.If this doesn't help,please provide your full code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemCreated="RadGrid1_ItemCreated">
           <MasterTableView DataKeyNames="OrderID">
               <Columns>
                  . . . . . . .
               </Columns>
               <DetailTables>
                   <telerik:GridTableView DataKeyNames="OrderID" runat="server">
                       <NoRecordsTemplate>
                           <asp:Label ID="NoRecordLabel" runat="server" />
                       </NoRecordsTemplate>
                   </telerik:GridTableView>
               </DetailTables>
           </MasterTableView>
       </telerik:RadGrid>

C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
       if (e.Item is GridNoRecordsItem)
       {
           GridNoRecordsItem norecordItem = (GridNoRecordsItem)e.Item;
           Label lbl = (Label)norecordItem.FindControl("NoRecordLabel");
           lbl.Text = "Your Text";
       }
   }

Thanks,
Princy
0
C
Top achievements
Rank 1
answered on 11 Sep 2013, 09:38 AM
ASCX:

<telerik:RadGrid ID="PartSettingsGrid" runat="server" Width="95%" ShowStatusBar="true"
AutoGenerateColumns="False" PageSize="10" AllowSorting="False" AllowMultiRowSelection="False"
AllowPaging="True" AllowCustomPaging="True">
    <pagerstyle mode="NextPrevAndNumeric"></pagerstyle>
    <mastertableview width="100%" datakeynames="PartId" allowmulticolumnsorting="False" >  
  <DetailTables>
              <telerik:GridTableView DataKeyNames="Id" Name="Texts" Width="100%" AllowCustomPaging="False" runat="server"><br>                            <NoRecordsTemplate><asp:Label ID="NoRecordLabel" runat="server" /></NoRecordsTemplate>
               <Columns>
   ...... 
       </Columns>
          </telerik:GridTableView>
     </DetailTables>
     <Columns>    
         .......
     </Columns> 
  </
mastertableview>
</telerik:RadGrid>


C#:

protected override void FrameworkInitialize()
{
   base.FrameworkInitialize();
   ......
   _partsGrid.ItemCreated += _partsGrid_ItemCreated;
   ......


protected void _partsGrid_ItemCreated(object sender, GridItemEventArgs e)
{
     .......
     if (e.Item is GridNoRecordsItem)
       {
           GridNoRecordsItem norecordItem = (GridNoRecordsItem)e.Item;
           Label lbl = (Label)norecordItem.FindControl("NoRecordLabel");
           lbl.Text = "Your Text";
       }
}

edit: Oh dear, I have no idea why everything ended up being on one row. Will try and fix.
edit2: Should be legible now.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Sep 2013, 11:22 AM
Hello,

Please try with the below link.

http://stackoverflow.com/questions/18722526/finding-label-in-gridtableview-radgrid

Thanks,
Jayesh Goyani
0
C
Top achievements
Rank 1
answered on 11 Sep 2013, 11:28 AM
It worked! Thanks a lot.
Tags
Grid
Asked by
C
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
C
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or