我有绑定列表到转发器(asp.net)
它显示如下
我想像这样绑定
我的代码
<asp:Repeater ID="RptID" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("T") %> :</td>
<td><%# Eval("D") %> :</td>
</tr>
</ItemTemplate>
</asp:Repeater>
C#
List<Options> pList = List Type of options;
RptID.DataSource = pList;
RptID.DataBind();
数据来源
public class Options
{
public string T { get; set; }
public string D { get; set; }
}
怎么做?
答案 0 :(得分:2)
您必须先将ItemDataBound
事件添加到Repeater。然后添加一个三元运算符,该运算符将评估具有前一个值previousValue
的全局字符串T
。
<asp:Repeater ID="RptID" runat="server" OnItemDataBound="RptID_ItemDataBound">
<ItemTemplate>
<tr>
<td><%# previousValue != Eval("T").ToString() ? Eval("T") + ":" : "" %></td>
<td><%# Eval("D") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
然后在代码后面添加OntItemDataBound方法和全局变量。
public string previousValue = "";
protected void RptID_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//cast the item back to a datarowview
DataRowView item = e.Item.DataItem as DataRowView;
//assign the new value to the global string
previousValue = item["T"].ToString();
}
或者如果绑定List<class>
,则需要执行此操作:
protected void RptID_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//cast the item back to its class
Options item = e.Item.DataItem as Options;
//assign the new value to the global string
previousValue = item.T;
}