[英]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. 然后,如果我在后面的代码中更改了
DataSourceID
,则转发器以相同的格式动态显示新数据。 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. 您可以在Repeater的OnItemCreated事件中获取列名。
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. 如果列数根据表而变化-那么您将需要研究使用嵌套转发器和一些后面的代码来控制它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.