Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child repeater's item count

Tags:

c#

asp.net

I am trying to get the child repeater's item count but for some reason it keeps coming up as zero. Here is my code: Parent repeater is rptDays. Child repeater is rptEditInfo.

protected void rptDays_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptEditInfo = (Repeater)e.Item.FindControl("rptEditInfo");
        ...
        DateTime thisDay = (DateTime)e.Item.DataItem;
        DataSet ds = new DataSet();
        ...
        ds = **bind valid dataset to this variable**
        rptEditInfo.DataSource = MRSTable;
        rptEditInfo.DataBind();
   }

}

protected void rptEditInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        Repeater rpt2 = (Repeater)((Repeater)e.Item.Parent);
        Repeater rpt1 = (Repeater)((Repeater)sender).Parent.FindControl("rptEditInfo");
        int countTest1 = rpt2.Items.Count //always zero
        int countTest2 = rpt1.Items.Count //always zero
    }
}

What am I doing wrong? The data is valid and populated. Only thing I can think of is that I am not accessing the child repeater properly.

like image 476
Lukas Avatar asked Dec 30 '25 01:12

Lukas


1 Answers

You cannot get the item count in the header(e.Item.ItemType == ListItemType.Header). The items will be created in this order:

  1. Header
  2. 1. Item ItemCreated (Count=0 since not databound yet)
  3. 1. Item ItemDataBound (Count=1 since first item created and databound)
  4. 2. Item ItemCreated (Count=1 since not databound yet)
  5. 2. Item ItemDataBound (Count=2 since second item created and databound) ....

So ItemDataBound is not the right stage to get the total count from the Items property. But always a better approach anyway is to use the datasource directly instead instead of counting Items(or Rows in a GridView).

protected void rptEditInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        Repeater rpt = (Repeater)sender;
        //note that this depends on your actual datasource, set a breakpoint and debug if you're unsure
        var dataSource = (DataView)rpt.DataSource; 
        int count = dataSource.Count;
    }
}
like image 173
Tim Schmelter Avatar answered Dec 31 '25 17:12

Tim Schmelter