Header with database value in repeater

sh su 1 Reputation point
2021-07-06T09:28:59.573+00:00

Hi I have a repeater control with header having database value.
But how can I show that value.
pls explain.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,066 Reputation points
    2021-07-07T06:38:54.43+00:00

    Hi @sh su ,
    The HeaderTemplate is for the entire repeater.The Repeater assumes that you have a different title for every row, so if you put the title just in the Header, it doesn't know which title to display. One solution is get the datatable's columnname and set in the ItemDataBound event.
    Just like this:

    <asp:Repeater ID="Repeater1" runat="server" O nItemDataBound="Repeater1_ItemDataBound">  
                    <HeaderTemplate>  
                        <table id="Table_car">  
                            <tr>  
                                <th>  
                                    <asp:Label ID="ID" runat="server"></asp:Label>  
                                </th>  
                                <th>  
                                    <asp:Label ID="Num" runat="server"></asp:Label>  
                                </th>  
                            </tr>      
                    </HeaderTemplate>  
                    <ItemTemplate>  
                        <tr>  
                            <td>  
                                <asp:Label ID="ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>  
                            </td>  
                            <td>  
                                <asp:Label ID="Nums" runat="server" Text='<%#Eval("Nums") %>'></asp:Label>  
                            </td>  
                        </tr>  
                    </ItemTemplate>  
                    <FooterTemplate>  
                        </table>  
                    </FooterTemplate>  
                </asp:Repeater>  
    
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
            {  
                if (e.Item.ItemType == ListItemType.Header)  
                {  
                    DataTable tbl = new DataTable();  
                    tbl.Columns.AddRange(new DataColumn[2] { new DataColumn("ID", typeof(int)),  
                                        new DataColumn("Nums", typeof(int))});  
                    tbl.Rows.Add(1, 1);  
                    tbl.Rows.Add(2, 2);  
                   //var tbl = (DataTable)((Repeater)sender).DataSource;  
                    SetHeaderValue(e.Item, "ID", tbl, 0);  
                    SetHeaderValue(e.Item, "Num", tbl,1);  
      
                }  
            }  
            protected void SetHeaderValue(RepeaterItem item, string litId, DataTable tbl, int colIndex)  
            {  
                var lit = item.FindControl(litId) as Label;  
                if (lit != null)  
                {  
                    string headerText = tbl.Columns[colIndex].ColumnName;  
                    lit.Text = headerText;  
                }  
            }  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments