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

ASP.NET
赫赫大名的A*寻路算法(vb.net版本)
asp.net(c#)下Jmai去说明 使用与下载
[原创]完美解决Could not load file or assembly ''AjaxPro.2'' or one of its dependencies. 拒绝访问。
asp.net下gridview 批量删除的实现方法
用CSS实现图片倾斜 只支持IE
.net get set用法小结
vs 不显示行号的操作方法
ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
ASP.Net不执行问题一解
asp.net 无限分类
让VS2008对JQuery语法的智能感知更完美一点
扩展方法ToJSON() and ParseJSON()
asp.net下PageMethods使用技巧
Linq to SQL Delete时遇到问题的解决方法
实现ASP.NET多文件上传程序代码
ASP.NET AJAX 1.0 RC开发10分钟图解
asp.net get set用法
ASP.NET下使用WScript.Shell执行命令
asp.net2.0实现邮件发送(测试成功)
Asp.net 无限级分类实例代码

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


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