Click here to Skip to main content
15,440,268 members
Articles / Programming Languages / C++
Technical Blog
Posted 8 Jun 2014

Tagged as

Stats

23.8K views
5 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.89/5 (3 votes)
8 Jun 2014CPOL
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)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

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. #SOreadytohelp

Comments and Discussions

 
-- There are no messages in this forum --