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

  1. usingSystem;
  2. usingSystem.Data;
  3. usingSystem.Configuration;
  4. usingSystem.Web;
  5. usingSystem.Web.Security;
  6. usingSystem.Web.UI;
  7. usingSystem.Web.UI.WebControls;
  8. usingSystem.Web.UI.WebControls.WebParts;
  9. usingSystem.Web.UI.HtmlControls;
  10. usingSystem.Collections.Generic;
  11. publicpartialclass_Default:System.Web.UI.Page
  12. {
  13. protectedvoidPage_Load(objectsender,EventArgse)
  14. {
  15. if(!IsPostBack)
  16. {
  17. Dictionary<string,A>d=newDictionary<string,A>();
  18. d.Add("aa",newA("1","xx"));
  19. d.Add("bb",newA("2","yy"));
  20. d.Add("cc",newA("3","zz"));
  21. DataList1.DataSource=d;
  22. DataList1.DataBind();
  23. }
  24. }
  25. }
  26. publicclassA
  27. {
  28. privatestringm_PKID;
  29. privatestringm_Type;
  30. publicstringPKID
  31. {
  32. get{returnm_PKID;}
  33. set{m_PKID=value;}
  34. }
  35. publicstringType
  36. {
  37. get{returnm_Type;}
  38. set{m_Type=value;}
  39. }
  40. publicA(){}
  41. publicA(stringpkid,stringtype)
  42. {
  43. this.m_PKID=pkid;
  44. this.m_Type=type;
  45. }
  46. }
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; } }
  1. <asp:DataListID="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)).ValueasA).PKID%>
  5. <%#(((System.Collections.Generic.KeyValuePair<string,A>)(Container.DataItem)).ValueasA).Type%>
  6. </ItemTemplate>
  7. </asp:DataList>
<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> 绑定到List的方式非常类似,这里不再给出代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值