如何绑定复杂字典在asp.net中的GridView? [英] How to bind a complex dictionary to a Gridview in asp.net?
问题描述
我已经使用了 字典&LT的早期绑定,字符串,字符串>
以一个gridview显示的url文本和其作为重点HREF - 值和它的作品就像一个魅力。
但因为我想要替换的字典与此一:
字典<字符串,LinkInfo>
绑定顺心!我要处理一些事件,如为onItemDataBound什么?
和 LinkInfo 结构是:
公共结构LinkInfo {
公共字符串的href;
公共BOOL启用;
}
是的,你需要subsribe到OnItemDataBound事件。使用模板字段,插入一个文本中包含HREF
一个ID在OnItemDataBound事件中使用
文字_href = e.Row.FindControl(HREF)作为文字;
如果(_href!= NULL)
{
_href =((LinkInfo)e.Row.DataItem).href;
}
修改:我已经写了进一步阐述这个主题的一篇文章:<一href=\"http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns\" rel=\"nofollow\">http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
I've used an early binding of a dictionary<string, string>
to a gridview to show Urls Text and its HRef as key-value and it works like a charm.
But since I want to replace the dictionary with this one :
dictionary<string, LinkInfo>
the binding goes wrong! Should I handle some events like as onItemDataBound or something? and the LinkInfo structure is:
public struct LinkInfo{
public string href;
public bool Enabled;
}
Yes, you need to subsribe to the OnItemDataBound event. Use a template field, insert a literal with an ID like "href"
In the OnItemDataBound event use
Literal _href = e.Row.FindControl("href") as Literal;
if(_href != null)
{
_href = ((LinkInfo)e.Row.DataItem).href;
}
Edit: I've written an article that elaborates this topic further: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
这篇关于如何绑定复杂字典在asp.net中的GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!