Codebureau - Matt Simner
(Not) just another .NET Developer
Monday, March 16, 2009
ASP.NET Data Binding - Accessing a parent data item from within a nested repeater
I'm maintaining an app at the moment that uses quite a few nested repeaters, and found that headers were being output when there was no data present. It was found that the header was being written in the ItemTemplate of an 'outer' repeater, rather than as the HeaderTemplate of the 'inner' repeater. The next problem was how to reference the outer repeater from the 'inner' HeaderTemplate...
The following will bind to a field called HeaderDescription.
<%# DataBinder.Eval(Container.Parent.Parent, "DataItem.HeaderDescription") %>
The parent of the inner item is it's repeater, so you have to go to it's parent to get the right RepeaterItem. Why don't you just do the following you ask?
<%# DataBinder.Eval(Container.Parent.Parent.DataItem, "HeaderDescription") %>
..'cos it doesn't work - The Eval method expects a 'Control' as its first parameter. There's other ways to do this server-side, but the first option is probably the easiest.
To complete the picture and only show when there's data you can add the following to the 'inner' repeater declaration
OnItemDataBound="ItemDataBound" Visible="false"
then..
protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
if (!e.Item.Parent.Visible)
e.Item.Parent.Visible = true;
}
}
This will ensure that you'll only show if you've bound a 'data' item (remember you're doing binding in the HeaderTemplate too). You could also hook similar things into other events, but it's generally more convenient to put these things into events that relate to the actual control (pre_render's probably another good candidate as it will only get called once and you can check the count in the DataSource).
Technorati Profile
.NET Framework
|
ASP.NET
|
C#
|
Web
posted on Monday, March 16, 2009 10:28:27 AM (AUS Eastern Daylight Time, UTC+11:00)
Comments [0]
Related posts:
LINQ Group by MAX Date Query
Simple WPF Page Navigation From an MVVM ViewModel
Use SQL Server Trusted Connections with ASP.NET on Windows 2003 without impersonation
Fixing SharePoint error: No item exists at [url]?ID=n. It may have been deleted or renamed by another user
LINQ to SQL Connection Strings with Class Library and Web.Config
Returning New Autonumber ID from Microsoft Access using ADO.NET and @@IDENTITY
Comments are closed.
Navigation
Codebureau Home
Matt Simner - T-Shirts, Software, Design
CodeProject Articles
Geek Casuals T-Shirts
On this page
Archive
<
August 2010
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
July, 2010 (2)
June, 2010 (2)
May, 2010 (2)
April, 2010 (3)
March, 2010 (1)
February, 2010 (2)
January, 2010 (1)
October, 2009 (2)
September, 2009 (1)
August, 2009 (1)
July, 2009 (1)
June, 2009 (3)
May, 2009 (1)
April, 2009 (3)
March, 2009 (2)
February, 2009 (2)
January, 2009 (3)
December, 2008 (4)
November, 2008 (3)
October, 2008 (4)
September, 2008 (3)
August, 2008 (3)
July, 2008 (16)
June, 2008 (10)
May, 2008 (1)
April, 2008 (6)
March, 2008 (4)
February, 2008 (7)
January, 2008 (10)
December, 2007 (2)
November, 2007 (3)
September, 2007 (3)
August, 2007 (5)
July, 2007 (5)
June, 2007 (4)
April, 2007 (1)
March, 2007 (2)
February, 2007 (2)
January, 2007 (6)
July, 2006 (1)
March, 2006 (1)
July, 2005 (2)
June, 2005 (1)
May, 2005 (3)
February, 2005 (3)
Month View
Categories
.NET Framework
Agile
Articles
ASP.NET
C#
CMS
Creative Design
CRM
dasBlog
Database
Development Process
DotNetNuke
FavPal.NET
InfoPath
IT Musings
JQuery
LINQ
Miscellaneous
Networking
Off piste
Oracle
Performance
Redbubble
Refactoring
Security
Setup and Deployment
SharePoint
SQL Server
SubVersion
Tools
Unit Testing
Usability
Visual Studio
Web
Workflow
WPF
XML
Blogroll
Brad Abrams
Charlie Poole (NUnit)
Roy Osherove
Scott Hanselman
SQL Authority - Pinal Dave