zoukankan      html  css  js  c++  java
  • 关于Repeater控件的使用

    关于Repeater控件的使用
    Repeater控件是一个数据显示控件,该控件允许通过为列表中显示的每一项重复使用指定的模板来自定义布局。
    要显示数据,必须先创建模板来绑定数据列表,模块定义如下(另见SDK):

    一.模板说明

    HeaderTemplate
    在所有数据绑定行呈现之前呈现一次的元素。典型的用途是开始一个容器元素(如表)。
     注意: HeaderTemplate 项不能是数据绑定的。

    FooterTemplate
    在所有数据绑定行呈现之后呈现一次的元素。典型的用途是关闭在 HeaderTemplate 项中打开的元素(使用 </table> 这样的标记)。
    注意: FooterTemplate 不能是数据绑定的。 

    AlternatingItemTemplate
    与 ItemTemplate 元素类似,但在 Repeater 控件中隔行(交替项)呈现一次。通过设置 AlternatingItemTemplate 元素的样式属性,可以为其指定不同的外观。 

    ItemTemplate
    为数据源中的每一行都呈现一次的元素。若要显示 ItemTemplate 中的数据,请声明一个或多个 Web 服务器控件并设置其数据绑定表达式以使其计算为 Repeater 控件(即容器控件)的 DataSource 中的字段。以下示例显示一个示例声明,它显示包含 Label 控件中的第一个名称的字段。

    First Name:
    <asp:Label runat="server"
    Text="<%# Container.DataItem.FirstName %>" />

    SeparatorTemplate
    在各行之间呈现的元素,通常是分行符(<br> 标记)、水平线(<hr> 标记)等。
    注意 SeparatorTemplate 项不能是数据绑定的。


    :该控件是不能通过可视化编辑模板的,而DataList的DataGrid控件就可以。



    二.下面说一下程序的创建过程
    1、 创建一个WEB应用程序,将默认的WEB窗体改名为:Repeater.aspx。
    2、 切换到“HTML”视图,输入下列代码:

    <%@ Page language="c#" Codebehind="Repeater.aspx.cs" AutoEventWireup="false" Inherits="TeachShow.Charpter7.Repeater" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

    <HTML>

    <HEAD>

    <title>Repeater</title>

    <LINK rel="stylesheet" type="text/css" href="../Style.css">

    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

    <meta name="CODE_LANGUAGE" Content="C#">

    <meta name="vs_defaultClientScript" content="JavaScript">

    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

    </HEAD>

    <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">

    <div align="center">

    <center>

    <table border="0" cellpadding="0" cellspacing="0" width="272" height="136">

    <tr>

    <td width="272" height="136">

    <div align="center">

    <center>

    <table border="1" cellpadding="0" cellspacing="0" width="272" height="60" bordercolorlight="#000000"

    bordercolordark="#ffffff" class="smallRed">

    <asp:Repeater id="Repeater1" runat="server">

    <HeaderTemplate>

    <tr>

    <td width="90" height="30"><FONT face="宋体">数字</FONT></td>

    <td width="91" height="30"><FONT face="宋体">平方</FONT></td>

    <td width="91" height="30"><FONT face="宋体">立方</FONT></td>

    </tr>

    </HeaderTemplate>

    <ItemTemplate>

    <tr>

    <td width="90" height="30"><%# DataBinder.Eval(Container.DataItem,"数字") %></td>

    <td width="91" height="30"><%# DataBinder.Eval(Container.DataItem,"平方") %></td>

    <td width="91" height="30"><%# DataBinder.Eval(Container.DataItem,"立方") %></td>

    </tr>

    </ItemTemplate>

    </asp:Repeater>

    </table>

    </center>

    </div>

    </td>

    </tr>

    </table>

    </center>

    </div>

    </form>

    </body>

    </HTML>


    解释一下程序中用到的方法
    DataBinder.Eval()方法:该方法用于在运行时计算数据绑定表达式,并且根据浏览器的需要来格式化输出结果。该方法有三个参数:

    A、 数据项的命名容器:命名容器是一个对象引用,该对象即是计算表达式所针对的对象。如果绑定是针对列表控件(如Repeater、DataList或DataGrid)的,则命名容器将始终是Container.DataItem。如果绑定是针对页面的,则命名容器是Page。

    B、 数据字段名:绑定表格的列名(此例如“平方”等)。

    C、 格式字符串

    如果要求高性能,不建议使用DataBinder.Eval()方法


    3、 打开Repeater.aspx.cs文件,输入下面的代码:

    using System;

    using System.Collections;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Web;

    using System.Web.SessionState;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.HtmlControls;


    namespace TeachShow.Charpter7

    {

    /// <summary>

    /// Repeater 的摘要说明。

    /// </summary>

    public class Repeater : System.Web.UI.Page

    {

    protected System.Web.UI.WebControls.Repeater Repeater1;

    private void Page_Load(object sender, System.EventArgs e)

    {

    // 在此处放置用户代码以初始化页面

    if(!this.IsPostBack)

    {

    DataTable mydt=new DataTable();

    DataRow mydr;

    mydt.Columns.Add(new DataColumn("数字",typeof(Int32)));

    mydt.Columns.Add(new DataColumn("平方",typeof(Int32)));

    mydt.Columns.Add(new DataColumn("立方",typeof(Int32)));


    for(int i=0;i<=10;i++)
    {

    mydr=mydt.NewRow();

    mydr[0]=i;   mydr[1]=i*i;   mydr[2]=i*i*i;
      
    mydt.Rows.Add(mydr);

    }

    this.Repeater1.DataSource=mydt;

    this.Repeater1.DataBind();
    }

    }

    #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e)
    {
       // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
      // InitializeComponent(); base.OnInit(e); }
     /// <summary>
     /// 设计器支持所需的方法 - 不要使用代码编辑器修改
     /// 此方法的内容。
     /// </summary>

    private void InitializeComponent()
    {

     this.Repeater1.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.Repeater1_ItemCommand);

    this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion private void Repeater1_ItemCommand
    (object source, ystem.Web.UI.WebControls.RepeaterCommandEventArgs e)
     { }
     }
    }

    4、 最后浏览,看看有什么结果?

    见下表:

    数字 平方 立方 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000


    原文出处:http://www.nqqn.com/ym/41/9307.htm
  • 相关阅读:
    10 个深恶痛绝的 Java 异常。。
    为什么公司宁愿 25K 重新招人,也不给你加到 20K?原因太现实……
    推荐一款代码神器,代码量至少省一半!
    Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。
    hdu 3853 LOOPS(概率 dp 期望)
    hdu 5245 Joyful(期望的计算,好题)
    hdu 4336 Card Collector(期望 dp 状态压缩)
    hdu 4405 Aeroplane chess(概率+dp)
    hdu 5036 Explosion(概率期望+bitset)
    hdu 5033 Building (单调栈 或 暴力枚举 )
  • 原文地址:https://www.cnblogs.com/xiaolin/p/589456.html
Copyright © 2011-2022 走看看