当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 使用 ASP+ 列表绑定控件(中)

ASP.NET
asp.net图片加水印
Asp.Net中页面运行时动态载入的UserControl内元素的事
ASP.NET底层架构探索之再谈.NET运行时(二)
借助封装类实现线程调用带参方法
面向对象设计思想(C#)
asp.net URL重写(URLRewriter) 简化版
GUID在.net里的使用,就用System.Guid结构
不要忽略c#中的using和as操作符
C#中ref和out的使用小结
C#的Web XML编程
asp.net2.0下 如何实现服务器端压缩包自解压
javascript如何调用C#后台代码中的过程 和ASP.NET调用
在ASP.NET中自动给URL加上超链接
ASP.NET 中处理页面“回退”的方法
ASP.NET的四种错误机制
asp.net跳转页面的三种方法比较
ASP.NET2.0中将GridView导出到Excel文件中
ASP.NET 2.0中GridView无限层复杂表头的实现
ASP.NET 2.0 中动态添加 GridView 模板列
十天学会ASP.net之第一天

ASP.NET 中的 使用 ASP+ 列表绑定控件(中)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 25 ::
收藏到网摘: n/a

Repeater1Page 类覆盖了 Page 类的 OnLoad 方法。此表示在对该页
的首次请求中调用 DataBind。这将导致对这些页上的数据绑定表达式求
值并使 repeater 控件列举数据源以及创建其项目。仅在首次请求时调用
DataBind 方法。这之所以能正常工作是因为 repeater能够在从前一次保
存状态的回传过程中重新创建其项目,而无需数据源实例。

  此页将类型ICollection 的公用属性显露出来。这将在设置repeater
的 DataSource 属性值的数据绑定表达式中使用。属性的获取实现使用包
含一组SiteInfo对象序列的ArrayList。此属性是公用的,因为只有页类
的公用和保护成员可在数据绑定表达式中使用。

  每个SiteInfo 对象有两个属性:SiteName 和 SiteURL。当对模板中
的HyperLink 控件进行数据绑定时将访问这些属性。在此控件的绑定表达
式中,Container.DataItem 表示要将特定项绑定到其上的单个 SiteInfo
对象。DataBinder.Eval(Container.DataItem, "SiteName") 访问当前
SiteInfo 对象的 SiteName 属性。

  Repeater1 示例向您介绍了几个基本概念:

●定义模板
●模板中的数据绑定语法和数据绑定表达式
●将 ArrayList 的 ICollection 表示用作数据源
●在最初处理页的过程中调用 DataBind 方法

DataList 控件

  DataList控件是一个模板化控件,它提供使用样式属性可视化地格式
化其表示的能力。它也可以产生多列布局。

摘自 DataList1.aspx:

〈%@ Page language="C#" src="DataList1.cs" inherits="Samples.
DataList1Page"%〉
...

〈asp:DataList runat=server id="peopleDataList"
 RepeatColumns="2" RepeatDirection="Vertical" RepeatMode="Table"
 Width="100%"〉

 〈property name="AlternatingItemStyle"〉
  〈asp:TableItemStyle BackColor="#EEEEEE"/〉
 〈/property〉
 〈template name="ItemTemplate"〉
  〈asp:Panel runat=server font-size="12pt" font-bold="true"〉
   〈%# ((Person)Container.DataItem).Name %〉
  〈/asp:Panel〉
  〈asp:Label runat=server Width="20px"
   BorderStyle="Solid" BorderWidth="1px" BorderColor="Black"
   BackColor='〈%# ((Person)Container.DataItem).FavoriteColor
   %〉'〉
  〈/asp:Label〉
  
  〈asp:Label runat=server Font-Size="10pt"
   Text='〈%# GetColorName(((Person)Container.DataItem).
   FavoriteColor) %〉'〉
  〈/asp:Label〉
 〈/template〉
〈/asp:DataList〉

此 .aspx 文件显示了用来生成此示例的 DataList 的声明。

  在此示例中,DataList 的多列布局是通过将 RepeatColumns 属性设
置为“2”来实现的。将RepeatDirection设置为“Vertical”会使项目从
上到下、然后从左到右排列。相反,值设置为“Horizontal”会导致项目
从左到右、然后从上到下排列。

  aspx语法包含对少数几种DataList的样式属性的设置。在此示例中,
DataList的Width被设置为其父级的100%。设置具灰色背景的Alternating
ItemStyle是为了获得带有条纹的外观。此示例还说明模板可以包含任意
复杂的控件定义,以满足在每个项目内获得理想布局的需要。

  最后此模板中的数据绑定表达式通过将Container.DataItem转换为其
类型来使用前期绑定。这不会招致与使用DataBinder.Eval(如 Repeater1
中所示)相关联的后期绑定的代价。但是,这种方法可能会产生可读性较
差的表达式。以下示例还给出了一个调用GetColorName方法(该方法是在
本页有代码支持的文件中实现的)的表达式示例。

DataList1.cs:

namespace Samples {
  ...

  public class DataList1Page : Page {
    protected DataList peopleDataList;

    protected string GetColorName(Color c) {
      return
       TypeDescriptor.GetConverter(typeof(Color)).Convert
       ToString(c);
    }

    private void LoadPeopleList() {
      // 创建数据源
      Person[] people = new Person[] {
        new Person("Nikhil Kothari", Color.Green),
        new Person("Steve Millet", Color.Purple),
        new Person("Chris Anderson", Color.Blue),
        new Person("Mike Pope", Color.Orange),
        new Person("Anthony Moore", Color.Yellow),
        new Person("Jon Jung", Color.MediumAquamarine),
        new Person("Susan Warren", Color.SlateBlue),
        n