是否有任何方法可以在数据绑定asp中的字段更改中显示子标题行:转发器控件,例如:
代替
country | colour | number uk | red | 3 uk | green | 3 france | red 3
做这个:
==UK== colour | number red | 3 green 3 ==FRANCE== colour | number red | 3
非常感谢任何帮助.
解决方法
没有内置支持,但这并不意味着它是不可能的.
你需要覆盖OnItemDataBound事件,并在标记中有类似的东西:
<asp:Repeater OnItemDataBound="NextItem" ... > <ItemTemplate><asp:Literal Id="Header" Visible="False" Text="{0}<strong>{1}</strong><br/><table>" ... /> <tr> <td><asp:Label id="Color" Text="<%# Eval("Color")" ... /></td> <td><asp:Label id="Number" Text="<%# Eval("Number")" ... /></td> </tr> </ItemTemplate> </asp:Repeater></table>
然后在代码隐藏中:
private string CurCountry = string.Empty; private void NextItem(object sender,RepeaterItemEventARgs e) { if ( e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return; DbDataRecord row = (DbDataRecord)e.Item.DataItem; if (CurCountry != row["country"].ToString() ) { string prev = (CurCounter == string.Empty)?"":"</table>"; CurCountry = row["country"].ToString(); Literal header = (Literal)e.Item.FindControl("Header"); Literal footer = (Literal)e.Item.FindControl("Footer"); header.Text = string.Format(header.Text,prev,CurCountry); header.Visible = true; } }