问题描述
我已经将一个 字典< string,string>
早期绑定到gridview,以显示Urls Text及其HRef作为键价值,它的作用就像一个魅力。
但是由于我想用这个替换字典:
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>
绑定错误!我应该处理像onItemDataBound这样的事情吗?
和 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;
}
推荐答案
是的,到OnItemDataBound事件。使用模板字段,插入带有href的ID的文字
Yes, you need to subsribe to the OnItemDataBound event. Use a template field, insert a literal with an ID like "href"
在OnItemDataBound事件中使用
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!