Search code examples
asp.netrepeater

ASP.net Repeater How to change header template caption server side?


I want to change the text "Employer Info" to something else depending o the state of certain variables. How do I get a reference to the caption so I can change its text?

<asp:Repeater ID="rptEmployers" runat="server">
  <HeaderTemplate>
    <table class="rotoTable1">
      <caption class="rotoTableCaption1">Employer Info</caption>
  </HeaderTemplate>


Solution

  • You could use the ItemDataBound of the Repeater.

    <asp:Repeater ID="rptEmployers" runat="server" OnItemDataBound="rptEmployers_ItemDataBound">
        <HeaderTemplate>
            <table class="rotoTable1">
                <caption class="rotoTableCaption1">
                    <asp:Literal ID="EmployerCaptionLabel" runat="server" Text="Employer Info"></asp:Literal>
                </caption>
        </HeaderTemplate>
    

    The in the code behind, set the caption to whatever you need:

    protected void rptEmployers_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            var employerCaption = (Literal)e.Item.FindControl("EmployerCaptionLabel");
            employerCaption.Text = "<your caption here>";
        }
    }