Problem: Nested Repeater won't databind()?

C

Colin Nicholls

Platform: ASP.NET 1.1

I have a repeater nested inside another repeater. My outer repeater is
looping fine. I am manually binding the inner repeater to a DataReader
obtained from another function.

My objects aren't null, my ItemDataBound() method is being called, and as
you can see I have verified that I have data.

Yet, the repeater does not appear in the final HTML, and what's more, after
the DataBind() call, the .Items.Count property is still 0. What am I doing
wrong?

Here is the code (I apologise for any text wrapping - is there a way to turn
that off?):

//------------------------------------------
protected void OuterRepeater_ItemDataBound( object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e )
{

System.Web.UI.WebControls.ListItemType lt = e.Item.ItemType ;

if ( lt == System.Web.UI.WebControls.ListItemType.Item ||
lt == System.Web.UI.WebControls.ListItemType.AlternatingItem )
{

System.Web.UI.WebControls.Repeater innerRepeater ;

System.Data.Common.DbDataRecord dr = e.Item.DataItem as
System.Data.Common.DbDataRecord ;
if ( dr != null)
{
string cChildKeyValue = dr["CHILD_KEY"].ToString() ;

System.Data.SqlClient.SqlDataReader oReader =
this.getChildDataFor( cChildKeyValue ) ;
if ( oReader != null )
{
innerRepeater = e.Item.FindControl("InnerRepeater") as
Repeater ;
if ( rptrGlassPart != null )
{
// Prove we have the right data:
if ( false ) {
while( oReader.Read() )
{
Response.Write( "<div>Data: " +
oReader.GetString(0) + "</div>" );
}
}
else {
// These statements appear to have no effect.
// The repeater is not rendered in the output HTML:

innerRepeater.DataSource = oReader ;
innerRepeater.DataBind() ;

// innerRepeater.Items.Count = 0 at this point.
}
}
else {
Response.Write("<div>innerRepeater is null.</div>") ;
}
oReader.Close();
}
}
}
}
//------------------------------------------

Thanks in advance,
- Colin
 
K

Karl Seguin

I can't say that I see anything inherently wrong, I imagine that the
innerRepeater is actually named rptrGlassPart as you still have a reference
to it in your code. You could use some better try/catch and god only
knows where your connections are being closed for the inner repeater's
datareader...but that shouldn't be a problem. I'd also wonder if using a
dataset with 2 datatables and a DataRelation wouldn't make things more clear
(and maybe even more performant) but again I can't see that as being the
actual problem.

Perhaps its soemthing wrong with the html side of the repeater?

Karl
 
C

Colin Nicholls

I can't say that I see anything inherently wrong, I imagine that the
innerRepeater is actually named rptrGlassPart as you still have a
reference to it in your code.

oops! Yes, sorry, I simplified the posted code and missed a reference.

You could use some better try/catch and god only
knows where your connections are being closed for the inner repeater's
datareader...but that shouldn't be a problem.

I took out some of the try/catch and debug code for simplification. The
connections (several in total) are all being closed in Page_Unload().

I'd also wonder if using a dataset with 2 datatables and a DataRelation
wouldn't make things more clear (and maybe even more performant) but
again I can't see that as being the actual problem.

My associate suggested that as well, but this is a simplified case of a
multi-child entity problem so changing to related data sets wouldn't solve
everything. I just felt that this should work - inefficient as it may be -
and it wasn't.

Perhaps its soemthing wrong with the html side of the repeater?

If so, I can't figure it out. I have other (not nested) repeaters elsewhere
in my aspx that are working fine, binding to DataReaders using the same
mechanism.

Thanks for verifying that it's not a simple newbie error. I have several
options to try.

1) bind the innerRepeater to a dataset instead of a reader (who knows? might
work)

2) use an inner DataGrid instead of a Repeater, bind it to the Reader
(ditto)

3) work up a REALLY simple nested Repeater example to see if it shows the
same problem.

Thanks again, Karl,
- Colin
 

lcr

Joined
Jul 8, 2008
Messages
1
Reaction score
0
Handling OnItemDataBound for nested repeater programmatically

Probably too late to help you with this issue Colin but maybe this will help others.

I've been having this same problem for ages with nested repeaters - couldn't get any of the events for the "child" repeater to fire from code behind and had to populate the repeater's values with <%DataBinder.Eval...%> on the apsx page which wasn't possible for more complicated databinding.

I finally found that adding the name of the event handler in the "OnItemDataBound" attribute of the repeater on the aspx page made the event fire in the code behind.

e.g. <asp:Repeater ID="rptCustomSectionLine" OnItemDataBound="rptCustomSectionLine_ItemDataBound" Runat="server">

Cheers
Lisa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top