How to bind a complex dictionary to a Gridview in asp.net?

落爺英雄遲暮 提交于 2019-12-24 07:38:35

问题


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;
}

回答1:


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



来源:https://stackoverflow.com/questions/2985554/how-to-bind-a-complex-dictionary-to-a-gridview-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!