asp:repeater - headers at section change

后端 未结 5 933
无人共我
无人共我 2020-12-21 05:14

is there any way to bring up a subheader row at changes in a field in an databound asp:repeater control e.g.

Instead of

country | colour | number
uk | red         


        
5条回答
  •  [愿得一人]
    2020-12-21 05:58

    There's no built-in support, but that doesn't mean it's impossible.

    You'll need to over-ride the OnItemDataBound event, and have something like this in markup:

    
        
             
                 
                 
             
        
    
    

    Then in the code-behind:

    private string CurCountry = string.Empty;
    
    private void NextItem(object sender, RepeaterItemEventARgs e)
    {
        if ( e.Item.ItemType != ListItemType.Item 
          && e.Item.ItemType != ListItemType.AlternatingItem) return;
    
        DbDataRecord row = (DbDataRecord)e.Item.DataItem;
    
        if (CurCountry != row["country"].ToString() )
        {
            string prev = (CurCounter == string.Empty)?"":"";
            CurCountry = row["country"].ToString();
    
            Literal header = (Literal)e.Item.FindControl("Header");
            Literal footer = (Literal)e.Item.FindControl("Footer");
    
            header.Text = string.Format(header.Text, prev, CurCountry);
            header.Visible = true;
        }
    }
    

提交回复
热议问题