• 【源码】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>

    运行结果图:

  • 相关阅读:
    采用多种算法,模拟摇奖:从1-36中随机抽出8个不重复的数字
    有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
    5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
    集合相关题目0927
    输入输出作业
    IO流,File类的测试........课堂加总结
    使用泛型............课堂
    Map 映射
    Set
    List相关知识点.......课堂加整理
  • 原文地址:https://www.cnblogs.com/handboy/p/7158313.html
Copyright © 2020-2023  润新知