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

ASP.NET
使用函数传递参数来执行相应的数据库操作
如何实现在窗体和窗体之间进行传递数据
ASP.NET中文显示之两种解决方法
ASP.NET、JSP及PHP之间的抉择
ASP.NET 2.0发送电子邮件中存在的问题
谈谈HtmlControl与WebControl的区别与用途
从ASP.NET 1.1升级到ASP.NET 2.0要考虑的Cookie问题
通过系统配置来提高ASP.NET应用程序的稳定性
妙用ASP2.0中的URL映射改变网址
AJAX实现web页面中级联菜单的设计
ASP.NET跨页面传值技巧总结
再议ASP.NET DataGrid控件中的“添加新行”功能
Geometry 对象浅析
重构CollapsibleSplitter
如何利用.NET Framework使用RSS feed
ASP.NET获取IP与MAC地址的方法
在ASP.NET 2.0中使用样式、主题和皮肤
ASP.NET中为GridView添加删除提示框
ASP.NET 2.0,无刷新页面新境界
看看一个.net版对话框控件

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


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