如何将复杂的字典绑定到asp.net中的Gridview?
问题描述:
我已经使用了一个dictionary<string, string>
的早期绑定到一个gridview来显示Urls文本及其HRef作为键值,它的作用就像一个魅力。 但因为我想更换字典与此一:如何将复杂的字典绑定到asp.net中的Gridview?
dictionary<string, LinkInfo>
结合顺心!我应该处理一些像onItemDataBound之类的事件吗? 和LinkInfo结构为:
public struct LinkInfo{
public string href;
public bool Enabled;
}
答
是的,你需要subsribe到OnItemDataBound事件。使用模板字段中插入一个文本中包含“HREF”
在OnItemDataBound事件的ID使用
Literal _href = e.Row.FindControl("href") as Literal;
if(_href != null)
{
_href = ((LinkInfo)e.Row.DataItem).href;
}
编辑:我已经写了阐述这个话题进一步的一篇文章:http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
我不想使用模板字段,但你告诉我这个代码的方式。谢谢 – Mehdi 2010-06-07 07:08:22