简体   繁体   中英

Display column name and column value dynamically in desired style in Repeater ASP.NET control

All i need is to display data in a repeater or any other control like this:

<asp:Repeater ID="MyRepeater" runat="server" DataSourceID="MyDT">
    <ItemTemplate>
        <asp:Label Text="NAME OF FIRST COLUMN" runat="server" ID="NameLabel" />
        <br />
        <asp:Label Text="VALUE OF FIRST COLUMN" runat="server" ID="ValueLabel" />
        <hr />
    </ItemTemplate>
</asp:Repeater>

and then if i changed DataSourceID in code behind, repeater dynamically display new data in the same format. Why i need to do this? because there are many tables in my data base and i don't want to design a repeater for each table.

thank you very much.

You can get the column name in the OnItemCreated event of the Repeater.

eg

DataRowView rowview = (DataRowView)e.Item.DataItem;
DataRow row = rowview.Row;
foreach (DataColumn col in row.Table.Columns)
   {
      nameOfColumn = col.ColumnName;
      // then add that to the relevant control in the row
   }

If the number of columns is variable depending on table - then you will need to look at using nested repeaters and some code behind to control those.

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