简体   繁体   中英

C# Get first repeater item column value to use on HeaderTemplate

I'm trying to get a column value of the first element on a repeater, and use it on the repeater's HeaderTemplate. After searching and trying many ways of achieving this through intellisense, I gave up and decided to post this question here.

My code is as follows:

Frontend

<asp:Repeater ID="states" runat="server">
    <HeaderTemplate>
        <h1>Stage: <asp:Literal ID="stageName" runat="server"></asp:Literal></h1>
        <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li><%# DataBinder.Eval(Container.DataItem, "StateName") %></li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

Backend

protected void BindStageStates()
     {
       List<StagesStatesModel> statesList = App.Services.Stages.StagesService.GetStatesByStage(Page.DefaultApp, 1, Page.DefaultCultureId).Where(p => p.StateActive == true).ToList();
       states.ItemDataBound += new RepeaterItemEventHandler(rptStagesStatesDataBound);
       states.DataSource = statesList;
       states.DataBind();
     }

void rptStagesStatesDataBound(object sender, RepeaterItemEventArgs e)
     {
         if (e.Item.ItemType == ListItemType.Header)
         {
             Literal stageName = (Literal)e.Item.FindControl("stageName");
             stageName.Text = // Something to go here..
         }    
     }

Thanks in advance!

Try OnItemCreated

protected void Repeater_OnItemCreated(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {    
          e.Item.FindControl(ctrl);
        }
        if (e.Item.ItemType == ListItemType.Header)
        {
          e.Item.FindControl(ctrl);
        }
    }

By: How to find controls in a repeater header or footer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM