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

ASP.NET
AspNetPager与Socut.Data使用方法
asp.net UpdaeProgress的简单用法
asp.net ajaxControlToolkit ValidatorCalloutExtender的简单用法
asp.net 简易生成注册码(数字+大小写字母)
asp.net中利用ashx实现图片防盗链代码
ASP.NET程序中常用代码汇总
ASP.NET 2.0/3.5中直接操作Gridview控件插入新记录
ASP.NET Ajax级联DropDownList实现代码
ASP.NET 2.0写无限级下拉菜单
asp.net Web Services上传和下载文件(完整代码)
asp.net DataGrid控件中弹出详细信息窗口
Asp.NET 多层登陆实现代码
利用Asp.Net回调机制实现进度条
ASP.NET Ref和Out关键字区别分析
Javascript调用Webservice的多种方法
.Net下的签名与混淆图文分析
.Net Compact Framework开发小技巧 推荐
.Net连接Oracle数据库的实现代码
js获取.aspx页面里面的服务器控件和.ascx中的服务器控件值
asp.net下 jquery jason 高效传输数据

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 34 ::
收藏到网摘: 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