Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 8 Jun 2014
Rate this:
Please Sign up or sign in to vote.
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:
    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!

License

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

Share

About the Author

Nitesh Luharuka
Team Leader Independent IT Professional, Consultant
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.
 
I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 8 Jun 2014
Article Copyright 2014 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid