Repeater

Repeater - 重复器,用来展示泛型集合中的数据

五大模板:
1、HeaderTemplate - 头模板,加载时会在开始执行一次
2、FooterTemplate - 脚模板,加载时会在最后执行一次
3、ItemTemplate - 项模板,数据源的数据有多少条就会重复多少次
4、AlternatingItemTemplate - 交替项模板,会与项模板交替执行,直到数据源中的数据重复完毕

后台绑定数据:
//1、需要把数据查出来
List<Users> list = new UsersData().SelectAll();

//2、绑定显示
Repeater1.DataSource = list; //数据指向,还未绑定
Repeater1.DataBind(); //真正的绑定数据

<%#  %>数据绑定,也调用可以直接调用函数;

Eval 后台数据显示;

<%# Eval("要显示的后台数据名称")%>

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        table
        {
            background-color:blue;
            width:100%;
        }
        #tr_head
        {
            color:white;
            font-size:20px;
            font-family:微软雅黑;
            text-align:center;
        }
        .aaa
        {
            background-color:white;
            text-align:center;
        }
        td
        {
            padding:10px;
        }
        #bbb
        {
            background-color:#e0e0e0;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server">
            <%-- 标头重复器,只走一遍 --%>
            <HeaderTemplate><table border="1">
                <tr id="tr_head">
                    <td>用户名</td>
                    <td>密码</td>
                    <td>昵称</td>
                    <td>性别</td>
                    <td>生日</td>
                    <td>民族</td>
                </tr>
                </HeaderTemplate>
            <%-- 各行走一次,与ItemTemplate轮流执行 --%>
            <AlternatingItemTemplate>
                 <tr class="aaa" id="bbb" style="<%#ShowRed() %> <%#ShowBold() %>">
                    <td style="15%"><%#Eval("UseName") %></td>
                    <td style="15%"><%#Eval("Pwd") %></td>
                    <td style="15%"><%#Eval("NateName") %></td>
                    <td style="15%"><%#SexName() %></td>
                    <td style="25%"><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td>
                    <td style="15%"><%#Eval("Nation") %></td>
                </tr>
            </AlternatingItemTemplate>
            <ItemTemplate>
                   <tr class="aaa" style="<%#ShowRed() %> <%#ShowBold() %>">
                    <td style="15%"><%#Eval("UseName") %></td>
                    <td style="15%"><%#Eval("Pwd") %></td>
                    <td style="15%"><%#Eval("NateName") %></td>
                    <td style="15%"><%#SexName() %></td>
                    <td style="25%"><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td>
                    <td style="15%"><%#Eval("Nation") %></td>
                </tr>
            </ItemTemplate>
            <%-- 尾部重复器,也只走一遍 --%>
            <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
    DataClassesDataContext context = new DataClassesDataContext();//实例化linQ类
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Users> lu = context.Users.ToList();//用泛型集合接受linq类里的数据

        Repeater1.DataSource = lu;//Repeater1里的数据指向范型集合lu
        Repeater1.DataBind();//数据绑定
    }
    //把true或false变成男或女
    public string SexName()
    {
        return Convert.ToBoolean(Eval("Sex")) ? "" : "";
    }
    //如果是女生,字体变成红色
    public string ShowRed()
    {
        string end = "";
        if (!Convert.ToBoolean(Eval("Sex")))
        {
            end = "color:red";
        }

        return end;
    }
    //1990年出生的字体加粗
    public string ShowBold()
    {
        string end = "";
        if(Convert.ToDateTime(Eval("Birthday")).Year==1990 )
        {
            end = "font-weight:bold";
        }

        return end;
    }
}

原文地址:https://www.cnblogs.com/fengsantianya/p/5680879.html