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

ContractedBlock.gif ExpandedBlockStart.gif 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;
    }
}

调用示例:

ContractedBlock.gif ExpandedBlockStart.gif 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)

转载于:https://www.cnblogs.com/stu-acer/archive/2009/11/19/1606319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于使用数据绑定控件DataList实现图书展示功能,以下是一个简单的示例代码及说明: 1. 数据源配置:首先需要配置一个数据源,可以使用SqlDataSource或者ObjectDataSource等控件来配置,这里以SqlDataSource为例: ``` <asp:SqlDataSource ID="sqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT [BookID], [BookName], [Author], [Price], [PublishDate] FROM [Books]"> </asp:SqlDataSource> ``` 2. DataList控件绑定:在ASP.NET页面中添加DataList控件,并设置DataSourceID属性为上述SqlDataSource的ID,同时设置ItemTemplate来定义每个数据项的展示方式。 ``` <asp:DataList ID="dataList" runat="server" DataSourceID="sqlDataSource"> <ItemTemplate> <div> <h4><%# Eval("BookName") %></h4> <p>作者:<%# Eval("Author") %></p> <p>价格:<%# Eval("Price", "{0:C}") %></p> <p>出版日期:<%# Eval("PublishDate", "{0:yyyy-MM-dd}") %></p> </div> </ItemTemplate> </asp:DataList> ``` 3. 控件样式设置:可以通过CSS样式来设置DataList控件的样式。 ``` <style type="text/css"> .book-item { border: 1px solid #ccc; margin: 10px; padding: 10px; width: 300px; height: 200px; float: left; } .book-item h4 { margin-top: 0; } </style> ``` 4. 数据分页:如果需要实现分页,可以使用DataPager控件来实现。 ``` <asp:DataPager ID="dataPager" runat="server" PagedControlID="dataList" PageSize="10"> <Fields> <asp:NumericPagerField ButtonCount="10" /> </Fields> </asp:DataPager> ``` 以上就是使用数据绑定控件DataList实现图书展示功能的一个简单示例,希望对你有所帮助。如果你还有其他问题,可以随时向我提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值