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.