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.
You cannot get the item count in the header(e.Item.ItemType == ListItemType.Header). The items will be created in this order:
1. Item ItemCreated (Count=0 since not databound yet)1. Item ItemDataBound (Count=1 since first item created and databound)2. Item ItemCreated (Count=1 since not databound yet)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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With