DataList 用法详解

article/2025/8/16 9:08:00
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataList.aspx.cs" Inherits="FileUpload自动上传文件.DataList" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title></title><style type="text/css">.style1{width:784px;}.style2{width:126px;}</style>
</head>
<body><form id="form1" runat="server"><div><asp:DataList ID ="dataList1" runat ="server" RepeatColumns ="1" GridLines ="Both"  RepeatLayout ="Table"   RepeatDirection ="Horizontal" onitemcommand="dataList1_ItemCommand" oneditcommand="dataList1_EditCommand" oncancelcommand="dataList1_CancelCommand" ondeletecommand="dataList1_DeleteCommand" onselectedindexchanged="dataList1_SelectedIndexChanged" onupdatecommand="dataList1_UpdateCommand"  DataKeyField ="Id"  ><HeaderTemplate ><%--页眉模版,和Repeater稍微不同,一般每个模版如果用到了table都应该在当前模版闭合,即当前模版用<table></table>包裹,而不是把<table>放在HeaderTemplate</table>放在FooterTemplate闭合,--%><table><%--因为如果按照后者做会使得RepeatColumns不生效,RepeatColumns意思的每行显示的项个数,即显示多少个ItemTemplate,一般只会显示一项因为这样每列的标题和每列的内容容易对齐,如果不对齐需要用样式设置每列的宽度使其对齐--%><tr class="style1"><th class="style2">标题1</th><th class="style2">标题2</th><th class="style2">标题3</th><th class="style2">标题4</th><th class="style2">时间</th></tr></table></HeaderTemplate><ItemTemplate><table ><tr class="style1"><td class="style2"><%#DataBinder.Eval(Container.DataItem,"title1")%></td><td class="style2"><%#DataBinder.Eval(Container.DataItem, "title2")%></td><td class="style2"><%#DataBinder.Eval(Container.DataItem, "title3")%></td><td class="style2"><%#DataBinder.Eval(Container.DataItem, "title4")%></td><td class="style2"><%#DateTime.Now.ToString()%></td><td class="style2" id ="td"><ul><li><asp:LinkButton id ="LinkButton2" runat ="server" CommandName ="edit" Text ="edit" ></asp:LinkButton></li><li><asp:LinkButton id ="LinkButton1" runat ="server" CommandName ="select" Text ="select" ></asp:LinkButton></li><li><asp:LinkButton id ="LinkButton3" runat ="server" CommandName ="delete" Text ="删除" ></asp:LinkButton></li></ul></td></tr></table></ItemTemplate><SelectedItemTemplate ><%--SelectedItemTemplate一般选中后需要呈现不同的控件才需要使用这个模版,如果只是样式不同只需要设置SelectedItemStyle--%><table ><tr class="style1"><td class="style2" style="color:Red;"><asp:TextBox ID ="txtTitle1" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title1")%>'></asp:TextBox></td><td class="style2"><asp:TextBox ID ="txtTitle2" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title2")%>'></asp:TextBox></td><td class="style2"><asp:TextBox ID ="txtTitle3" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title3")%>'></asp:TextBox></td><td class="style2"><asp:TextBox ID ="txtTitle4" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title4")%>'></asp:TextBox></td><td class="style2"><%#DateTime.Now.ToString()%></td><td class="style2" id ="td"><ul><li><asp:LinkButton id ="Linkcancel" runat ="server" CommandName ="cancelselect" Text ="取消选中" ></asp:LinkButton></li></ul></td></tr></table></SelectedItemTemplate><EditItemTemplate ><%--EditItemTemplate一般用于编辑时程序文本框给用户输入--%><table ><tr class="style1"><td class="style2" style="color:Red;"><asp:TextBox ID ="txtTitle1" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title1")%>'></asp:TextBox></td><td class="style2"><asp:TextBox ID ="txtTitle2" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title2")%>'></asp:TextBox></td><td class="style2"><asp:TextBox ID ="txtTitle3" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title3")%>'></asp:TextBox></td><td class="style2"><asp:TextBox ID ="txtTitle4" runat ="server" Text ='<%#DataBinder.Eval(Container.DataItem, "title4")%>'></asp:TextBox></td><td class="style2"><%#DateTime.Now.ToString()%></td><td class="style2" id ="td"><ul><li><asp:LinkButton id ="Linkupdate" runat ="server" CommandName ="update" Text ="更新" ></asp:LinkButton></li><li><asp:LinkButton id ="Linkcancel" runat ="server" CommandName ="cancel" Text ="取消" ></asp:LinkButton></li></ul></td></tr></table></EditItemTemplate><FooterStyle BackColor="#990000" Font-Bold="true" ForeColor="White" /><AlternatingItemStyle BackColor="White" /><ItemStyle BackColor="#FFFBD6" ForeColor="#333333" /><SeparatorStyle BorderStyle="Dashed" /><SelectedItemStyle BackColor="#FFCC66" Font-Bold="true" ForeColor="Navy" /><FooterTemplate ></FooterTemplate></asp:DataList></div></form>
</body>
</html>
View Code

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;namespace FileUpload自动上传文件
{public partial class DataList : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){BindRptData();}}private void BindRptData(){DataTable dt = new DataTable();dt.Columns.Add("Id");dt.Columns.Add("title1");dt.Columns.Add("title2");dt.Columns.Add("title3");dt.Columns.Add("title4");for (int i = 1; i < 5; i++){DataRow row = dt.NewRow();row["id"] = i;row["title1"] = "title1" + i;row["title2"] = "title2" + i;row["title3"] = "title3" + i;row["title4"] = "title4" + i;dt.Rows.Add(row);}dataList1.DataSource = dt;dataList1.DataBind();}protected void dataList1_ItemCommand(object source, DataListCommandEventArgs e){//所有的操作都可以放在这个方法进行,比如编辑,取消,更新,删除都可以放到此处进行,也可以用单独的方法,如dataList1_EditCommandswitch (e.CommandName.ToLower()){ case "select"://选中控件时如果使用了SelectedItemTemplate需要重新绑定数据,绑定数据可以在此处进行,也可以在dataList1_SelectedIndexChanged进行,若在此处进行需要设置dataList1.SelectedIndex = e.Item.ItemIndex;,因为此时dataList1.SelectedIndex为-1//dataList1.SelectedIndex = e.Item.ItemIndex;//BindRptData();break;//取消操作要根据取消的操作类型做不同动作,如取消选中应该把SelectedIndex设置为-1,取消编辑,应该把EditItemIndex设置为-1,并且设置后都要重新绑定数据case "cancelselect":dataList1.SelectedIndex = -1;BindRptData();break;}}protected void dataList1_EditCommand(object source, DataListCommandEventArgs e){//datalist编辑需要设置编辑索引,然后重新绑定数据,只要用到EditItemTemplate, SelectedItemTemplate这两个模版点击对应的操作时都要重新绑定数据dataList1.EditItemIndex = e.Item.ItemIndex;BindRptData();}protected void dataList1_CancelCommand(object source, DataListCommandEventArgs e){//这里只对应CommandName 为cancel的动作,每个方法都对应着特定的CommandName,如dataList1_EditCommand对应的CommandName为edit,//如果有多种取消动作则需要取不同的CommandName放到dataList1_ItemCommand方法去做
dataList1.EditItemIndex = -1;BindRptData();}protected void dataList1_SelectedIndexChanged(object sender, EventArgs e){BindRptData();}protected void dataList1_UpdateCommand(object source, DataListCommandEventArgs e){TextBox txtTitle1 = e.Item.FindControl("txtTitle1") as TextBox;TextBox txtTitle2 = e.Item.FindControl("txtTitle2") as TextBox;TextBox txtTitle3 = e.Item.FindControl("txtTitle3") as TextBox;TextBox txtTitle4 = e.Item.FindControl("txtTitle4") as TextBox;/*** 更新到数据库代码* * * * ***///更新后取消编辑状态,重新绑定数据显示到界面dataList1.EditItemIndex = -1;BindRptData();}protected void dataList1_DeleteCommand(object source, DataListCommandEventArgs e){//DataKeys 存放的是每行记录的主键,需要设置DataKeyField字段string id = dataList1.DataKeys[e.Item.ItemIndex].ToString();//删除当前记录,重新绑定数据//SqlCommand cmd = new SqlCommand("Delete From Union_User Where ID=" + id, conn);//cmd.ExecuteNonQuery();
BindRptData();}}
}
View Code

相关参考资料:

http://halesir.blog.163.com/blog/static/204612069201223174854257/

http://halesir.blog.163.com/blog/static/20461206920122317514384/

http://halesir.blog.163.com/blog/static/20461206920123865357191/

http://blog.csdn.net/ranlianjie/article/details/1618073

转载于:https://www.cnblogs.com/lidaying5/p/6187173.html


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

相关文章

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(页面完全加…

onload 和 onunload 事件

onload 和 onunload 事件会在用户进入或离开页面时被触发。onload 事件可用于检测访问者的浏览器类型和浏览器版本&#xff0c;并基于这些信息来加载网页的正确版本。onload 和 onunload 事件可用于处理 cookie。 onmousedown,onmouseup 以及 onclick 构成了鼠标点击事件的所有…