【问题标题】:How to bind a complex dictionary to a Gridview in asp.net?如何将复杂字典绑定到 asp.net 中的 Gridview?
【发布时间】:2011-02-28 10:58:53
【问题描述】:

我使用了 dictionary<string, string> 到网格视图的早期绑定来将 Urls Text 及其 HRef 显示为键值,它就像一个魅力。 但是因为我想用这个替换字典:

dictionary<string, LinkInfo>

绑定出错了!我应该处理诸如 onItemDataBound 之类的事件吗? LinkInfo 结构是:

public struct LinkInfo{
    public string href;
    public bool Enabled;
}

【问题讨论】:

    标签: c# asp.net data-binding gridview data-dictionary


    【解决方案1】:

    是的,您需要订阅 OnItemDataBound 事件。使用模板字段,插入 ID 为“href”的文字

    在 OnItemDataBound 事件中使用

    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

    【讨论】:

    • 我不想使用模板字段,但你用这段代码告诉我方法。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多