【源码】List<T>泛型绑定repeater,以及repeater的交替绑定

原文发布时间为:2009-10-28 —— 来源于本人的百度文章 [由搬家工具导入]


后台:

using System;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{

    public List<Model> listModel = new List<Model>();
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 4; i++)
        {
            Model model = new Model();
            model.Id = i + 10;
            model.Name = "Student" + i.ToString();
            model.Age = i + 20;
            listModel.Add(model);
        }
        for (int i = 0; i < 4; i++)
        {
            Response.Write("<font color='red'>" + listModel[i].Id.ToString() + "," + listModel[i].Name + "," + listModel[i].Age.ToString() + "</font><br/>");
        }
        repModel.DataSource = listModel;
        repModel.DataBind();

    }
}

[Serializable]
public class Model
{
    private int id;
    private string name;
    private int age;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div>
                前台获取ID:<%=listModel[2].Id %></div>
            <asp:Repeater ID="repModel" runat="server">
                <HeaderTemplate>
                    <ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <li>
                        <%#Eval("Id") %>
                        ,<%#Eval("Name") %>,<%#Eval("Age") %></li>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <li><font color="red">
                        <%#Eval("Id") %>
                        ,<%#Eval("Name") %>,<%#Eval("Age") %></font> </li>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </ul></FooterTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

运行结果图:

原文地址:https://www.cnblogs.com/handboy/p/7158313.html