DataList详细用法

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

1. DataList显示数据

例1:使用DataList显示数据   

Code
<asp:DataList ID="DataList1" runat="server" DataSourceID="srcMovies">
    
<ItemTemplate>
        
<h1><%#Eval("Title"%></h1>
        
<b>Directed by:</b><%#Eval("Director"%>
            
<br />
        
<b>Description:</b><%#Eval("Description"%>
    
</ItemTemplate>
</asp:DataList>

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

Code

<table id="DataList1" cellspacing="0" border="0" style="border-collapse:collapse;">
<tr>
    
<td>
        
<h1> 非常完美</h1> <b>Directed by:</b>依萌<br /><b>Description:</b> 两年前,立志成……
    
</td>
</tr>
<tr>
    
<td>
        
<h1> 罗马假日  </h1> <b>Directed by:</b>William Wyler<br /><b>Description:</b> 英国的安妮公主到罗马去访问,国务烦身
    
</td>
</tr>

</table>

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

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

Code
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" GridLines="Both" DataSourceID="srcMovies">
    
<ItemTemplate>
        
<h1><%#DataBinder.Eval(Container.DataItem,"Title"%></h1> <!-- 为种绑定数据的方法与上面一种是一样的,只是写法不同 -->
        
<b>Directed by:</b><%#Eval("Director"%>
            
<br />
        
<b>Description:</b><%#Eval("Description"%>
    
</ItemTemplate>
</asp:DataList>



3. 多列显示数据

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

Code
<script runat=”server”>
decimal totals;
protected 
void dlstMovies_ItemDataBound(object sender, DataListItemEventArgs e)
{
    
// 在ItemDataBound中找到某个列值的方法就是使用DataBinder.Eval。
    // e.Item就是一个DataList的Container。
    if (e.Item.DataItem != null)
        totals 
+= (decimal)DataBinder.Eval(e.Item.DataItem, "BoxOfficeTotals");
    
if (e.Item.ItemType == ListItemType.Footer)
    {
        Label lblTotal 
= (Label)e.Item.FindControl("lblTotal");
        lblTotal.Text 
= totals.ToString("c");
    }
}
</script>

<asp:DataList id="dlstMovies" DataSourceID="srcMovies" GridLines="Horizontal"
        UseAccessibleHeader
="true" OnItemDataBound="dlstMovies_ItemDataBound" CssClass="movies" Runat="server" >
    
<HeaderTemplate>
        Movie Box Office Totals
    
</HeaderTemplate>
    
<ItemTemplate>
        
<%#Eval("Title")%>:
        
<%#Eval("BoxOfficeTotals","{0:c}"%>
    
</ItemTemplate>
    
<FooterTemplate>
        
<b>Total:</b>
        
<asp:Label id="lblTotal" Runat="server" />
    
</FooterTemplate>
</asp:DataList>


4. DataList支持的模板

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

Code
<asp:DataList id="dlstMovieCategories" DataSourceID="srcMovieCategories" DataKeyField="Id"
        GridLines
="Both" CssClass="movies" Runat="server">
    
<ItemTemplate>
        
<asp:LinkButton id="lnkMovie" Text=’<%#Eval("Name") %>’ CommandName="Select" Runat="server" />
    
</ItemTemplate>
</asp:DataList>

<asp:DataList id="dlstMovieDetails" DataSourceID="srcMovieDetails" Runat="server">
    
<ItemTemplate>
        
<h1><%#Eval("Title")%></h1>
        Directed by: 
<%#Eval("Director"%>
            
<br />
        Box Office Totals: 
<%#Eval("BoxOfficeTotals","{0:c}"%>
    
</ItemTemplate>
</asp:DataList>

<asp:SqlDataSource id="srcMovieCategories" ConnectionString="<%$ ConnectionStrings:Movies %>"
    SelectCommand
="SELECT Id, Name FROM MovieCategories" Runat="server" />
<asp:SqlDataSource id="srcMovieDetails" ConnectionString="<%$ ConnectionStrings:Movies %>"
    SelectCommand
="SELECT Title,Director,BoxOfficeTotals FROM Movies WHERE CategoryId=@CategoryId" Runat="server">
    
<SelectParameters>
        
<asp:ControlParameter Name="CategoryId" ControlID="dlstMovieCategories" PropertyName="SelectedValue" />
    
</SelectParameters>
</asp:SqlDataSource>

 

5. DataList控件选择数据项

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

Code


6. DataList的事件

    DataList比较强大。它支持编辑、更新、删除、取消,虽然相比于GridView,它要写更多的代码,但是可定制性也更高了。
    DataList包括以下几个事件:
       ● CancelCommand: 对
DataList 控件中的某项单击 Cancel 按钮时发生。【数据项中要有一个Button,且CommandName为Cancel】
       ● EditCommand : 单击 Edit 按钮时发生。[数据项中要有一个Button,且CommandName为Edit]
       ● UpdateCommand : 单击更新按钮时发生 [数据项中要有一个Button,且CommandName为Update]
       ● 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>标签.


http://chatgpt.dhexx.cn/article/RgZ07u2t.shtml

相关文章

DataList 用法详解

<% Page Language"C#" AutoEventWireup"true" CodeBehind"DataList.aspx.cs" Inherits"FileUpload自动上传文件.DataList" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w…

datalist标签使用

jsp使用datalist标签写可查询可输入下拉框样式 jsp使用datalist标签写可查询可输入下拉框样式。需求用法 jsp使用datalist标签写可查询可输入下拉框样式。 需求 jsp页面实现可选择,可查询,可输入的下拉选择框, 一段废话 本来想用select看看有什么属性可以实现,但基本上接触的…

DataList内容详解

DataList是另一种显示数据控件&#xff0c;它与GridView不同的是&#xff0c;它全部使用模板进行设计&#xff0c;并且DataList的模板是对整行设置&#xff0c;而不是像GridView那样只对某一列进行模板设计。 正是由于它使用模板进行设计&#xff0c;所以它的灵活性比GridView更…

DataList控件详细用法(一)

使用DataList控件 本章内容&#xff1a; 1、理解事件冒泡 2、使用模板 3、在DataList中显示数据 4、在DataList中创建多列 5、捕获DataList控件中产生的事件 6、选择DataList中的项 7、使用DataList控件中的DataKeys集合 8、编辑DataList中的项 本章介绍在ASP.NET框架中功能…

(13)<datalist> 标签

一、<datalist>标签的作用 <datalist> 标签规定了<input> 元素可能的选项列表。<datalist>元素包含了一组<option>元素&#xff0c;这些元素表示预定义可选值&#xff0c;在<input>元素输入过程中&#xff0c;会自动响应<option>元…

Hibernate缓存的evict、clear和flush方法

evict()、clear()和flush()方法是Hibernate缓存的3种基本操作方法&#xff0c;本文主要介绍这3种方法的使用方式和具体区别。 Company表&#xff1a; Company实体类&#xff1a; import java.util.Set;public class Company {private int companyId;private String companyName…

注册中心日志输出_Running the evict task with compensationTime 0ms_频繁输出这句_SpringCloud工作笔记161

可以在:application.properties 中配置一下不停的打印日志,太烦人了.##Running the evict task with compensationTime 0ms不停的输出这个太烦人了可以关闭 logging.level.com.netflixwarn技术交流QQ群【JAVA,C,Python,.NET,BigData,AI】&#xff1a;170933152 开通了个人技术微…

SpringCloud Eureka注册中心日志输出问题:Running the evict task with compensationTime 0ms

动Eureka注册中心后&#xff0c;控制台一直输出 — [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms 这段日志&#xff0c;间隔时间与你的配置有关。强迫症看着很烦&#xff0c;那么在开发过程中如何关闭这条日…

HashMap中的putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)解读

在面试中我们会经常遇到关于HashMap的问题&#xff0c;这里我写了我对HashMap里面一个挺重要的方法 putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)的理解&#xff0c;下面就是我对这个方法的理解。 其实putVal(int hash, K key, V value, boolean o…

org.hibernate.Session.evict(Object object)方法的使用

在一个实体A、B的关联关系中&#xff0c;如下图&#xff1a; B外键关联A&#xff0c;关联字段A_id A中保存有B的集合blist 在A的实体属性blist&#xff0c;使用懒加载注解&#xff0c;如下: OneToMany(targetEntityB.class, cascade CascadeType.ALL, fetch FetchType.LAZY)…

mysql evict_Hibernate的flush()和evict()

/** * 测试uuid主键生成策略 */ public void testSave1() { Session session null; Transaction tx null; try { session HibernateUtils.getSession(); tx session.beginTransaction(); User1 user new User1(); user.setName("李四"); user.setPassword("…

Ceph Cache Tier中flush和evict机制源码分析

存储系统&#xff1a;ceph-14.2.22 操作系统&#xff1a;ubuntu-server-16.04.07 OSDService::agent_entry [ ceph/src/osd/OSD.cc ] OSD服务启动过程中会创建一个名为osd_srv_agent的线程&#xff0c;在分层存储中&#xff0c;该线程负责缓存池与数据池之间的数据迁移工作。该…

HashMap evict 放逐之旅

HashMap evict 放逐之旅 我不认识的evict小结 我不认识的evict 正在撸猫写代码的我&#xff0c;遇到了一个LinkedHashMap ConcurrentModifyException&#xff0c;真是让人头秃。问题排查过程也是费了一些力气&#xff08;手动狗头&#xff09;&#xff0c;最后发现是我异步请求…

Redis源码剖析之内存淘汰策略(Evict)

文章目录 何为Evict如何EvictRedis中的Evict策略源码剖析LRU具体实现LFU具体实现LFU计数器增长LFU计数器衰减 evict执行过程evict何时执行evict.c 总结 Redis作为一个成熟的数据存储中间件&#xff0c;它提供了完善的数据管理功能&#xff0c;比如之前我们提到过的 数据过期和…

WiredTiger系列2:Eviction详解

Eviction Evict的实质主要是将内存中的page淘汰出内存&#xff0c;简单来说&#xff0c;当cache里面的“脏页”达到一定比例或cache使用量达到一定比例时&#xff0c;wt就会触发相应的evict page线程来将pages&#xff08;包含干净的pages和脏pages&#xff09;按一定的算法&a…

onload事件

onload事件&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><script type"text/javascript">//onload事件的方法function onloadFun(){alert(静态注…

onUnload事件

1. 首先JavaScript会使我们有能力创建动态页面然后JavaScript是可以被侦测到事件行为的然而在网页中的每一个元素都将可以来产生某些可以触发JavaScript的函数的事件就可以打个比方说我们在用户中里面点击某个按钮时的产生的某一个onClick 事件来触发某个函数之后就在事件中在H…

script标签的onload事件的触发时机

onload事件在资源被加载完成后会被触发。对于script标签&#xff0c;在外部js文件被加载后代码会被立即执行。那么&#xff0c;外部js文件中的代码和该script标签的onload回调函数&#xff0c;它们的执行顺序是怎样的呢&#xff1f;没有找到官方的说明文档&#xff0c;所以自己…

load事件

javaScript中最常用到的一个事件就是load。当页面完全加载后&#xff08;包括所有图像、javaScript文件、css文件等外部资源&#xff09;&#xff0c;就会触发window上边的load事件。 window&#xff1a; window.addEventListener(load, function(e) {console.log(页面完全加…