Access Headertemplate and FooterTemplate of a Repeater in code

Posted by: Jotekes Blog, on 05 Jun 2007 | View original | Bookmarked: 0 time(s)

Asked on Forums. Point is that say you have a Repeater like this:

<asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <asp:Label ID="lblHeaderDisplay" runat="server" />
            </HeaderTemplate>
            <ItemTemplate>
                <br />
                Test<br />
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblFooterDisplay" runat="server" />
            </FooterTemplate>
        </asp:Repeater>

and you want to access Labels on header and footer in code. Of course, it's trivial that you can access them in ItemCreated - which is fine, only drawback being that viewstate isn't yet tracked at that point - or ItemDataBound - which probably works for almost all scenarios -

You could also use approach as follows, in case you need to access them elsewhere, if for some reason Itemxxx events don't work for you. You just need to have the Repeater databound before as otherwise header or footer templates aren't yet instantiated

...
Repeater1.DataBind();

        Label lblHeader=Repeater1.Controls[0].FindControl("lblHeaderDisplay") as Label;
        if (lblHeader != null)
            lblHeader.Text = "Hello world from header";

        Label lblFooter = Repeater1.Controls[Repeater1.Controls.Count -1].FindControl("lblFooterDisplay") as Label;
        if (lblFooter != null)
            lblFooter.Text = "Hello world from footer";

Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: DataGrid | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 5425 | Hits: 95

Similar Posts

  • Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 26: Authentication and Personalization more
  • What's for VB.NET in the Future, interesting to see... more
  • C# GridView Sorting/Paging w/o a DataSourceControl DataSource more
  • Accessing and Updating Data in ASP.NET 2.0: Programmatically Accessing Data using the Data Source Controls more
  • Understanding the naming container hierarchy of ASP.NET databound controls more
  • Accessing and Updating Data in ASP.NET 2.0: Declaratively Caching Data more
  • Accessing and Updating Data in ASP.NET 2.0: Examining the Data Source Control's Events more
  • Three New "Working with Data in ASP.NET 2.0" Tutorials Available more
  • From closures to prototypes, part 1 more
  • Accessing and Updating Data in ASP.NET 2.0: Retrieving XML Data with XmlDataSource Control more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD