June 6, 2014 by Nitesh

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

Friends,

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 of 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 –
          //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!

#ASP.Net#How To?#Repeater

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup