如何将复杂字典绑定到ASP.NET中的Gridview? [英] How to bind a complex dictionary to a Gridview in asp.net?

查看:103
本文介绍了如何将复杂字典绑定到ASP.NET中的Gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将一个 字典< 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;
}

编辑:我写了一篇文章进一步阐述此主题: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆