Introduction
While working with Repeater controls, we all have used the ItemDataBound
event to apply changes to specific records based on the data present in the record. Many times, we have used FindControl()
event to do so. In this post, we will see how can we find a control present in the HeaderTemplate
or FooterTemplate
of the Repeater control.
You can do it in 3 ways:
- Inside the
ItemDataBound()
Event handler – If you want to perform some operations based on data found. A sample code is given below:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Control ctrl = e.Item.FindControl("ctrlID");
}
else if (e.Item.ItemType == ListItemType.Footer)
{
Control ctrl = e.Item.FindControl("ctrlID");
}
}
- Inside the
ItemCreated()
Event handler – After the data has been bound to the database and you want to find a control in the repeater control, you should use this method. A sample code is given below:
protected void Repeater1_ItemCreated(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Control ctrl = e.Item.FindControl("ctrlID");
}
if (e.Item.ItemType == ListItemType.Header)
{
Control ctrl = e.Item.FindControl("ctrlID");
}
}
- If you want to access them after the Data has been bound, you can use the below piece of code:
Control ctrl = Repeater1.Controls[0].Controls[0].FindControl("ctrlID");
Control ctrl = Repeater1.Controls[repeater1.Controls.Count - 1].Controls[0].FindControl("ctrlID");
Keep learning and sharing! Cheers!