惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - shipfi

CMarkup的改进 - shipfi - 博客园 asp.net中的AJAX编程-异步网络调用 asp.net中的AJAX编程-Ajax服务器扩展 asp.net中的AJAX编程-Javascript语法补充 asp.net中的AJAX编程-ASP.NET AJAX框架 asp.net问题点集合 asp.net学习之ado.net(无连接模式中的DataAdapter) asp.net学习之ado.net(连接模式访问) asp.net学习之Repeater控件 asp.net学习之扩展GridView asp.net学习之GridView事件、GridViewRow对象 asp.net学习之GridView七种字段 asp.net学习之GridView asp.net学习之再论sqlDataSource asp.net学习之SqlDataSource asp.net学习之 数据绑定控件--表格绑定控件 asp.net学习之 数据绑定控件--List数据绑定控件 asp.net学习之数据绑定控件、数据源控件概述 理解T-SQL: 存储过程 - shipfi - 博客园
asp.net学习之DataList控件
shipfi · 2009-10-19 · via 博客园 - shipfi

     DataList控件与Repeater控件一样由模板驱动,与Repeater控件不同的是: DataList控件默认输出是一个HTML表格.DataList在输出时已经在相应的模板上套上了表格标签,而Repeater则是模板是什么样,输出就是什么样.

1. DataList显示数据

例1:使用DataList显示数据   

Code

    以上的例子,在Repeater控件显示数据时也是使用这样的格式,但是输出的HTML就有点不同了,DataList输出了一张HTML表格:

Code

2. 表格布局(Table)和流布局(Flow)

      ● RepeatLayout : 来确定是在表中显示还是在流布局中显示. 可选值为Flow和Table
    如果在上个例子中加入RepeatLayout属性为Flow,则输出如下所示:

Code


3. 多列显示数据

   从例1看出,默认的DataList是使用一行显示一项数据,但是可以通过以下属性让其一行显示多个数据项:
     ● RepeatColumn   : 需要显示的列数
     ● RepeatDirection : 输出网格时的方向,可以为Horizontal(横),Vertical(纵)
例2:多列显示数据

Code

4. DataList支持的模板

    除了ItemTemplate、AlternatingItemTemplate、SeparatorTemplate、HeaderTemplate、FooterTemplate外。
    DataList还支持另外两个模板:
      ● EditItemTemplate : 当行进入编辑状态时显示的样式
      ● SelectedItemTemplate : 当列被选中时显示的样式
例3:通过FooterTemplate进行数据汇总

Code

5. DataList控件选择数据项

    DataList有个只读属性,名为SelectedValue,通过它,可以知道哪个数据项被选中了。当然,需要事先设置好DataList的CommandName为Select才可以进行选择。
例4:使用DataList控件作为菜单使用

Code


6. DataList的事件

    DataList比较强大。它支持编辑、更新、删除、取消,虽然相比于GridView,它要写更多的代码,但是可定制性也更高了。
    DataList包括以下几个事件:
       ● CancelCommand: 对
DataList 控件中的某项单击 Cancel 按钮时发生。【数据项中要有一个Button,且CommandName为Edit】
       ● EditCommand : 单击 Edit 按钮时发生。[数据项中要有一个Button,且CommandName为Edit]
       ● UpdateCommand : 单击更新按钮时发生 [数据项中要有一个Button,且CommandName为Edit]
       ● DeleteCommand : 单击Delete按钮时发生 [数据项中要有一个Button,且CommandName为Delete]
       ● SelectIndexChanged: 单击Select按钮时发生 [数据项中要有一个Button,且CommandName为Select]
       ● ItemCommand: 单击任何按钮时发生 [数据项中要有一个Button,且CommandName为任意值]
    另外,还包括已经在Repeater控件中介绍过的DataBinding、ItemCreated、ItemDataBound事件。
    以上,具体的,可以查看MSDN中的一些例子,比较详细。

    对于DataList来说,可以设定其DataKeys属性,所以在一些事件中(如ItemCommand),可以直接使用 e.Item.ItemIndex来访问一个数据项的关键字索引。对于在什么事件中使用ItemIndex进行取得,什么事件中要用 DataBinder.Eval(Container.DataItem,"Id")这样的方式,要区分清楚【一般来说,在ItemDataBound事件时,用DataBinder方法获得,在ItemCommand事件中,用ItemIndex来获取】


    下面是MSDN中的一篇东东,介绍如何响应绑定控件中的按钮事件:

    如果您使用的是带模板的数据绑定控件(例如,DataList 或 FormView 控件),且模板包含 Button、LinkButton 或 ImageButton Web 服务器控件,则按钮可以将它们的 Click 事件转发给包含控件。这样,您可以包含按钮以实现尚未为数据绑定控件定义的自定义功能,例如,编辑、删除、更新和取消。

响应数据绑定控件中的按钮事件
      1. 在控件模板中添加 Button、LinkButton 或 ImageButton。
      2. 将按钮的 CommandName 属性设置为标识其功能的字符串,如“Sort”或“Duplicate”。
      3. 创建用于控件的 ItemCommand 事件的方法。在该方法中,执行下列操作: 
           a. 检查事件参数对象的 CommandName 属性来查看传入什么字符串。
           b. 为用户单击的按钮执行相应的逻辑。

    下面的示例演示响应 DataList 控件中的按钮单击的方法。在该示例中,ItemTemplate 包含显示购物车的 ImageButton 控件。该按钮发送命令 AddToCart。事件处理程序确定所单击的是哪个按钮,如果是购物车按钮,则执行相应的逻辑。

<script runat="server">
private 
void DataList1_ItemCommand(object source, 
    DataListCommandEventArgs e)
{
    
if (e.CommandName == "AddToCart")
    {
        
// Add code here to add the item to the shopping cart.
        // Use the value of e.Item.ItemIndex to find the data row
        // in the data source.
    }
}
</script>

5. 格式化DataList

    对于默认的DataList输出,肯定是比较难看的,所以要对它套用CSS式样,以输出符合我们意愿的格式.DataList提供了一些属性,通过它们,可以变更DataList的样式
      ● CssClass : DataList使用的CSS
      ● AlternatingItemStyle : 交替行使用的样式
      ● EditItemStyle : 编辑行使用的样式
      ● FooterStyle : 页脚样式
      ● HeaderStyle : 页眉样式
      ● ItemStyle  : 普通数据行样式
      ● SelectedItemStyle : 选中项的样式
      ● SpearatorStyle : 间隔行样式
      ● GridLines : 单元格边框格式,可以有"None,Horizontal,Vertical,Both”
      ● ShowFooter : 是否显示页脚
      ● ShowHeader : 是否显示页眉
      ● UseAccessibleHeader : 在页眉行的单元格中使用HTML标签<th>来替换<td>标签.