Problem: Nested Repeater won't databind()?

Discussion in 'ASP .Net' started by Colin Nicholls, Jan 12, 2005.

  1. 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
     
    Colin Nicholls, Jan 12, 2005
    #1
    1. Advertising

  2. Colin Nicholls

    Karl Seguin Guest

    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
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Colin Nicholls" <> wrote in message
    news:eT0fGaM%...
    > 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
    >
    >
     
    Karl Seguin, Jan 12, 2005
    #2
    1. Advertising

  3. "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:e35kFnM%...

    > 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
     
    Colin Nicholls, Jan 12, 2005
    #3
  4. Colin Nicholls

    lcr

    Joined:
    Jul 8, 2008
    Messages:
    1
    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
     
    lcr, Jul 8, 2008
    #4
  5. Colin Nicholls

    codelavie

    Joined:
    Nov 9, 2009
    Messages:
    1
    Thanks Lisa

    Thanks Lisa, it helped me.
     
    codelavie, Nov 9, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    860
  2. Arjen
    Replies:
    1
    Views:
    473
    Curt_C [MVP]
    Aug 17, 2005
  3. Iain
    Replies:
    0
    Views:
    528
  4. Replies:
    2
    Views:
    2,585
  5. Michael
    Replies:
    0
    Views:
    226
    Michael
    Dec 29, 2003
Loading...

Share This Page