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

Accessing controls in a NoRecordsTemplate block from code behind

2 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 12 Sep 2012, 10:02 AM
I'm trying to access the controls within a NoRecordsTemplate block in my code behind. Doing a search in these forums shows the following code snippet:

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    GridNoRecordsItem norecordItem = (GridNoRecordsItem)RadGrid1.MasterTableView.GetItems(GridItemType.NoRecordsItem)[0];
    Label lbl = (Label)norecordItem.FindControl("Label1");
}

However, when I try this, the GetItems method returns no items. My GridView control has definitely got NoRecordsTemplate section.

Any ideas why it can't find this?

Thanks for your help,
Dan

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 12 Sep 2012, 10:20 AM

Hi Dan,

Please make sure that you have set the 'ShowHeadersWhenNoRecords' to true. Otherwise you can access the NoRecordTemplate in the ItemCreated event as follows.

C#:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridNoRecordsItem)
{
GridNoRecordsItem nitem = (GridNoRecordsItem)e.Item;
Label lbl = (Label)nitem.FindControl("label");
}
}

Thanks,
Princy.

0
Dan
Top achievements
Rank 1
answered on 12 Sep 2012, 10:33 AM
Great! The ItemCreated is what I wanted and it works perfectly. Thanks for your help! :)
Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dan
Top achievements
Rank 1
Share this question
or