数据列表DataList模板之实例

article/2025/8/16 9:01:14

1数据列表DataList与重复列表Repeator很类似,但是DataList应用更广泛,因为他可以选择和修改数据项的内容。

DataList的数据显示和布局与Repeator控件一样都是通过“模板”控制的。

(注:模板至少要定义一个“数据项模板”(ItemTemplate)来指定显示布局)

 

2DataList支持的模板类型:

模板

名称

说明

ItemTemplate

数据项模板

必需的,它定义了数据项及其表现形式

AlternatingItemplate

数据项交替模板

为了使相邻的数据项能够有所区别,可以定义交替模板,它使得相邻的数据项看起来明显不同,缺省情况下,他和ItemTemplate模板定义一致,即却剩下相邻数据无表示区分

SeparatorTemplate

分隔符模板

定义数据项之间的分隔符

SelectedItemTemplate

选中项模板

定义被选择的数据项的表现内容与布局形式,当未定义“SelectedItemTemplate”模板时,选中项的表现内容与形式无特殊化,由ItemTemplate模板定义所决定

EditItemTemplate

修改选项模板

定义即将被修改的数据项的显示内容与布局形式,缺省情况下,修改选项模板就是数据项模板(ItemTemplate)的定义

HeaderTemplate

报头定义模板

定义表头变现形式

FooterTemplate

表尾定义模板

 

实例:
用到的数据库charge_sys中的student_Info表:

studentNo

studentName

grade

cash

1

韩学杨

大一

123.000

2

韩寒

大二

123.000

3

韩红

大三

222.000

4

韩学敏

大四

20000.000

 
程序在Default.aspx中:
Default.aspx中:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>DataList控件练习</title>
</head>
<body><form id="form1" runat="server"><div><!--添加控件“DataList1”,并设置属性--><asp:DataList ID="DataList1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand" onitemcommand="DataList1_ItemCommand" onupdatecommand="DataList1_UpdateCommand" Width="308px"><%--修改选项模板--%><EditItemTemplate> <!--添加“LinkButton2”按钮,并设置ComandName="update"--><asp:LinkButton ID="LinkButton2" runat="server" CommandName="update">保存</asp:LinkButton><!--添加“LinkButton3”按钮,并设置CommandName="cancel"--><asp:LinkButton ID="LinkButton3" runat="server" CommandName="cancel">取消</asp:LinkButton><!--数据绑定,绑定字段为学号“studentNo”--><%#DataBinder.Eval(Container.DataItem,"studentNo") %><!--添加“txtGrade”文本框,并绑定字段年级grade--><asp:TextBox ID="txtGrade" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"grade") %>'></asp:TextBox></EditItemTemplate><%--表尾模板样式 --%> <FooterStyle Font-Bold="True" Font-Italic="False" Font-Size="18pt" /> <%--报头模板样式 --%><HeaderStyle Font-Bold="True" Font-Size="18pt" /><%--数据项模板样式--%><ItemStyle ForeColor="#000066" /><%--报头模板--%> <HeaderTemplate>模板页眉<br /> <hr /></HeaderTemplate><%--数据项模板--%><ItemTemplate><!--添加“lBtnShowDetails”按钮,并设置CommandName="select"--><asp:LinkButton ID="lBtnShowDetails" runat="server" CommandName="select">查看详细信息</asp:LinkButton><!--添加“LinkButton1”按钮--><asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">编辑</asp:LinkButton><!--绑定学号studentNo字段--><%#DataBinder.Eval(Container.DataItem,"studentNo") %></ItemTemplate><%--选中项模板--%><SelectedItemTemplate>学号:<!--绑定学号studentNo字段--><%#DataBinder.Eval(Container.DataItem,"studentNo") %>           <br /> 姓名:<!--绑定姓名studentName字段--><%#DataBinder.Eval(Container.DataItem,"studentName") %><br /> 金额:<!--绑定金额cash字段--><%#DataBinder.Eval(Container.DataItem,"cash") %></SelectedItemTemplate><%--表尾模板 --%> <FooterTemplate><hr /><br />模板页脚</FooterTemplate></asp:DataList></div></form>
</body>
</html>

 

效果图:


 

Default.aspx.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){if (!this.IsPostBack){this.DataBindToDataList();}}//用来绑定到DataList控件的方法  private void DataBindToDataList(){SqlConnection con = DB.createCon();SqlDataAdapter sda = new SqlDataAdapter();sda.SelectCommand = new SqlCommand("select * from student_Info", con);DataSet ds = new DataSet();sda.Fill(ds, "studentInfo");this.DataList1.DataKeyField = "cardno";this.DataList1.DataSource = ds.Tables["studentInfo"];this.DataList1.DataBind();}protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e){if (e.CommandName == "select"){this.DataList1.SelectedIndex = e.Item.ItemIndex;this.DataBindToDataList();       //调用方法DataBindToDataList()}}protected void DataList1_EditCommand(object source, DataListCommandEventArgs e){this.DataList1.EditItemIndex = e.Item.ItemIndex;this.DataBindToDataList();}protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e){this.DataList1.EditItemIndex = -1;this.DataBindToDataList();}protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e){string cardNo = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();string grade = ((TextBox)e.Item.FindControl("txtGrade")).Text;SqlConnection con = DB.createCon();SqlCommand cmd = new SqlCommand("update student_Info set grade='" + grade + "' where cardno='" + cardNo + "'", con);con.Open();cmd.ExecuteNonQuery();this.DataList1.EditItemIndex = -1;this.DataBindToDataList();}
}

运行结果:

页面加载:


 

 点击“查看详细信息”按钮后:

点击“编辑”按钮后:

 

总结:“动手”才是硬道理

 


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

相关文章

DataList详细用法

DataList控件与Repeater控件一样由模板驱动,与Repeater控件不同的是: DataList控件默认输出是一个HTML表格.DataList在输出时已经在相应的模板上套上了表格标签,而Repeater则是模板是什么样,输出就是什么样. 1. DataList显示数据 例1:使用DataList显示数据 Code <as…

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;所以自己…