吉吉于

Asp.Net笔记(3):Datalist简单入门

DataList 控件,类似于 Repeater 控件,用于显示限制于该控件的项目的重复列表。不过,DataList 控件会默认地在数据项目上添加表格。DataList 控件可被绑定到数据库表、XML 文件或者其他项目列表。

<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="dtlEmployee" runat="server" BackColor="#000" ForeColor="#ffffff">
            <ItemTemplate>
                <table>
                    <tr>
                        <td><b>NO.</b><%#Eval("Id") %></td>
                    </tr>
                    <tr>
                        <td><b>Name:</b><%#Eval("Name") %></td>
                        <td><img alt='<%#Eval("Name") %>' src='Image/<%#Eval("Picpath") %>'/></td>
                    </tr>
                    <tr>
                        <td><b>Description:</b><%#Eval("Description") %></td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Employee emp = new Employee(1,"Lazynight","z.gif","夜阑,声如窃,独难解。");
        Employee emp2 = new Employee(2, "Flowerowl", "z.gif", "夜阑,声如窃,独难解。");
        Employee emp3 = new Employee(3, "Sinking Ship", "z.gif", "夜阑,声如窃,独难解。");
        List<Employee> lst = new List<Employee>();
        lst.Add(emp);
        lst.Add(emp2);
        lst.Add(emp3);
        this.dtlEmployee.DataSource = lst;
        this.dtlEmployee.DataBind();
    }
}

 

public class Employee
{
    private int _id;
    private string _name;
    private string _picpath;

    public string Picpath
    {
        get { return _picpath; }
        set { _picpath = value; }
    }
    private string _description;

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
    public Employee()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 职工类
    /// </summary>
    /// <param name="id">职工号</param>
    /// <param name="name">职工姓名</param>
    /// <param name="description">个人描述</param>
    public Employee(int id, string name, string picpath,string description)
    {
        this.Id = id;
        this.Name = name;
        this.Picpath = picpath;
        this.Description = description;
    }
}

 

 

 

 

转载请注明:于哲的博客 » Asp.Net笔记(3):Datalist简单入门