Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

How To Find Controls present in HeaderTemplate or FooterTemplate of Repeater Control in ASP.NET

4.89/5 (3 votes)
8 Jun 2014CPOL 26.1K  
How To Find Controls present in HeaderTemplate or FooterTemplate of Repeater Control in ASP.NET

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:
    C#
    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:
    C#
    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:
    C#
    //For Header
    Control ctrl = Repeater1.Controls[0].Controls[0].FindControl("ctrlID");
    //For Footer
    Control ctrl = Repeater1.Controls[repeater1.Controls.Count - 1].Controls[0].FindControl("ctrlID");

Keep learning and sharing! Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)