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

 
 
  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Web;   
  5. using System.Web.Security;   
  6. using System.Web.UI;   
  7. using System.Web.UI.WebControls;   
  8. using System.Web.UI.WebControls.WebParts;   
  9. using System.Web.UI.HtmlControls;   
  10. using System.Collections.Generic;   
  11. public partial class _Default : System.Web.UI.Page    
  12. {   
  13.     protected void Page_Load(object sender, EventArgs e)   
  14.     {   
  15.         if (!IsPostBack)   
  16.         {   
  17.             Dictionary<string, A> d = new Dictionary<string, A>();   
  18.             d.Add("aa"new A("1""xx"));   
  19.             d.Add("bb"new A("2""yy"));   
  20.             d.Add("cc"new A("3""zz"));   
  21.   
  22.             DataList1.DataSource = d;   
  23.             DataList1.DataBind();   
  24.         }   
  25.     }   
  26. }   
  27.   
  28. public class A   
  29. {   
  30.     private string m_PKID;   
  31.     private string m_Type;   
  32.   
  33.     public string PKID   
  34.     {   
  35.         get { return m_PKID; }   
  36.         set { m_PKID = value; }   
  37.     }   
  38.   
  39.     public string Type   
  40.     {   
  41.         get { return m_Type; }   
  42.         set { m_Type = value; }   
  43.     }   
  44.   
  45.     public A() { }   
  46.   
  47.     public A(string pkid, string type)   
  48.     {   
  49.         this.m_PKID = pkid;   
  50.         this.m_Type = type;   
  51.     }   
  52. }  
  1. <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">  
  2.     <ItemTemplate>  
  3.         <%#( (System.Collections.Generic.KeyValuePair<string, A>)(Container.DataItem)).Key %>  
  4.         <%#( ((System.Collections.Generic.KeyValuePair<string, A>)(Container.DataItem)).Value as A).PKID %>  
  5.         <%#( ((System.Collections.Generic.KeyValuePair<string, A>)(Container.DataItem)).Value as A).Type %>  
  6.     </ItemTemplate>  
  7. </asp:DataList>  
绑定到List的方式非常类似,这里不再给出代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值