惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
T
Tailwind CSS Blog
博客园_首页
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
J
Java Code Geeks
量子位
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
Check Point Blog
V
Visual Studio Blog
H
Help Net Security
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta

博客园 - stu_acer

对C#对象的Shallow、Deep Cloning认识【转】 PowerShell 启动应用程序【转】 把boolean 参数放到最后面(Put boolean arguments last)【转载】 几个收藏的根据数据库生成Insert语句的存储过程[修正版] SQL Server删除外键约束 IE6下PNG图片透明显示解决方法(From QQ) 用Regex类计算一个字符串出现次数是最好方法【转载】 C#有关Session 操作的几个误区【转】 - stu_acer - 博客园 重写Asp.NET2.0 TreeView的折叠/展开方法(TreeView_ToggleNode) javascript window.close() 去掉那讨厌的确认对话框【转】 thickbox使用技巧【转】 - stu_acer - 博客园 jQuery操作checkbox的例子(全选,反选,获取选取值)【转】 查看SQL Server 2005版本号 创建SQL索引,查看SQL性能语句收集 ddlevelsmenu在IE6下,与select冲突的解决办法【整理】 IE6 Select元素无法被div等元素覆盖的bug解决办法【zz】 利用Attribute和反射从模板生成短信 - stu_acer - 博客园 Session & Cookie 的使用建议(zz) - stu_acer AdventureWorks、Northwind、pubs示例数据库下载
DataList绑定数据到泛型类(Dictionary) 【转】
stu_acer · 2009-11-19 · via 博客园 - stu_acer

Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            Dictionary
<string, A> d = new Dictionary<string, A>();
            d.Add(
"aa"new A("1""xx"));
            d.Add(
"bb"new A("2""yy"));
            d.Add(
"cc"new A("3""zz"));

            DataList1.DataSource 

= d;
            DataList1.DataBind();
        }
    }
}
public class A
{
    
private string m_PKID;
    
private string m_Type;public string PKID
    {
        
get { return m_PKID; }
        
set { m_PKID = value; }
    }
public string Type
    {
        
get { return m_Type; }
        
set { m_Type = value; }
    }
public A() { }public A(string pkid, string type)
    {
        
this.m_PKID = pkid;
        
this.m_Type = type;
    }
}

调用示例:

Code
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">  
    
<ItemTemplate>  
        
<%#( (System.Collections.Generic.KeyValuePair<string, A>)(Container.DataItem)).Key %>  
        
<%#( ((System.Collections.Generic.KeyValuePair<string, A>)(Container.DataItem)).Value as A).PKID %>  
        
<%#( ((System.Collections.Generic.KeyValuePair<string, A>)(Container.DataItem)).Value as A).Type %>  
    
</ItemTemplate>  
</asp:DataList> 


原文:DataList绑定数据到泛型类(Dictionary)