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.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>
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 = ; i < ; 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_EditCommand switch (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 = -;
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 = -;
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 = -;
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();
}
}
}
相关参考资料:
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
DataList 用法详解的更多相关文章
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- mysql中event的用法详解
一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...
- CSS中伪类及伪元素用法详解
CSS中伪类及伪元素用法详解 伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...
- c++中vector的用法详解
c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...
- AngularJS select中ngOptions用法详解
AngularJS select中ngOptions用法详解 一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...
- systemctl命令用法详解
systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...
- CSS3的@keyframes用法详解:
CSS3的@keyframes用法详解:此属性与animation属性是密切相关的,关于animation属性可以参阅CSS3的animation属性用法详解一章节. 一.基本知识:keyframes ...
随机推荐
- 【Mybatis架构】输入、输出映射
前言综述: 其实在我们分析Mybatis的查询缓存或者是一些简介的时候,我们就不难看到有关于Mybatis输入输出映射的东西,比如说: 但是一直没有想起来系统的来总结一下这方面的相关知识,偶然看到 ...
- 爱特梅尔ATMEL全系列芯片解密单片机破解
爱特梅尔ATMEL全系列芯片解密单片机破解 ATMEL芯片介绍: ATMEL公司为全球性的业界领先企业,致力于设计和制造各类微控制器.电容式触摸解决方案.先进逻辑.混合信号.非易失性存储器和射频 (R ...
- java导出word(带图片)
public class CreateWordDemo { public void createDocContext(String file) throws DocumentException,IOE ...
- python 函数传递方式
在python中方法传递的参数到底是值传递还是引用传递? 1. 首先需要知道python中变量的类型:Python的变量分为可变变量和不可变变量. 针对于不可变的类型比如string int def ...
- Spring泛型依赖注入
1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package ...
- HDU 3308 LCIS(线段树)
题目链接 模板题吧,忘了好多,终于A了... #include <cstring> #include <cstdio> #include <string> #inc ...
- 什么是BFC?(转载)
在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很 ...
- ZeroMQ接口函数之 :zmq_init - 初始化ZMQ环境上下文
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_init zmq_init(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_init - 初 ...
- 李洪强iOS经典面试题141-报错警告调试
李洪强iOS经典面试题141-报错警告调试 报错警告调试 你在实际开发中,有哪些手机架构与性能调试经验 刚接手公司的旧项目时,模块特别多,而且几乎所有的代码都写在控制器里面,比如UI控件代码.网络 ...
- stl文件格式
http://wenku.baidu.com/view/a3ab7a26ee06eff9aef8077b.html [每个三角形面片的定义包括三角形各个定点的三维坐标及三角形面片的法矢量[三角形的法线 ...