《ASP.NET1200例》ASP.Net 之Datalist数据删除(支持批量)
.aspx
<div>
<asp:DataList ID="DataList1" runat="server" Width="355px"
onitemcommand="DataList1_ItemCommand" DataKeyField="id">
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:Label ID="Label3" Width="70px" runat="server" Text="ID"></asp:Label>
<asp:Label ID="Label4" Width="170px" runat="server" Text="imageUrl"></asp:Label>
<asp:Label ID="Label5" runat="server" Text="handle"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Label ID="Label1" Width="70px" runat="server" Text='<%# Eval( "id") %>'></asp:Label>
<asp:Label ID="Label2" Width="170px" runat="server" Text='<%# Eval("imageUrl") %>'></asp:Label>
<asp:Button ID="Button1" runat="server" CommandName="singleDelete" Text="delete" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button2" runat="server" CommandName="mutlDelete" Text="delete" />
</FooterTemplate>
</asp:DataList>
</div>
.aspx.cs
public partial class _234DeleteData : System.Web.UI.Page
{
ShowImageBll showImageBll = new BLL.ShowImageBll(); protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
} private void BindDataList()
{
DataSet ds = showImageBll.GetList();
DataList1.DataSource = ds;
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "singleDelete":
int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
if (showImageBll.GetList()!=null)
{
showImageBll.DeleteSingleList(id);
Response.Write("<script>alert('删除成功!')</script>");
BindDataList();//重新绑定数据库
}
else
{
showImageBll.DeleteSingleList(id);
Response.Write("<script>alert('删除失败!')</script>");
}
break;
case "mutlDelete": DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象
for (int i = ; i < dlic.Count; i++)
{
if (dlic[i].ItemType == ListItemType.AlternatingItem || dlic[i].ItemType == ListItemType.Item)
{
CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
if (cb.Checked)
{
int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
showImageBll.DeleteSingleList(id1);
}
}
}
BindDataList();//重新绑定数据库
break; } }
}
总结:
【1】数据源读取Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' 可简化为 Text='<%# Eval( "id") %>'
【2】int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());//读取当前列的key值
【3】 DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象
《ASP.NET1200例》ASP.Net 之Datalist数据删除(支持批量)的更多相关文章
- 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定
aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...
- 《ASP.NET1200例》在DataList里编辑和删除数据
学习内容:如何创建一个支持编辑和删除数据的DataList.增加编辑和删除功能需要在DataList的ItemTemplate和EditItemTemplate里增加合适的控件,创建对应的事件处理,读 ...
- 《ASP.NET1200例》<asp:DataList>分页显示图片
aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...
- 《ASP.NET1200例》ListView 控件与DataPager控件的结合<二>
ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示 为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己 ...
- 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>
分页 在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...
- 《ASP.NET1200例》<ItemTemplate>标签在html里面有什么具体的作用
严格的来说 <ItemTemplate> 在html中无意义,他只是针对诸如 Repeater.DataList.GridView中的一个模板 至于里面的含义,你可以这样想,既然Repea ...
- 《ASP.NET1200例》C#在网页上编写动态时钟
包含Timer类的命名空间有3个 Timer Class (System.Threading) Timer Class (System.Windows.Forms) 一般用于窗体程序 Timer ...
- 《ASP.NET1200例》高亮显示ListView中的数据行并自动切换图片
aspx <script type="text/javascript"> var oldColor; function SetNewColor(Source) { ol ...
- 《ASP.NET1200例》ListView控件之修改,删除与添加
aspx <body> <form id="form1" runat="server"> <div> <asp:Lis ...
随机推荐
- Eclipse-将svn上的项目转化成相应的项目
这里假设svn上的项目为maven项目 首先从svn检出项目 其中项目名称code可自己定义更改新的名称 从svn检出的项目结构 然后将项目转化成相关的项目 转换加载中 加载/下载 maven相关内容 ...
- Yii2 radioList设置默认值
可以在对应的Controller的action中设置 $model->type = 1; 在view中 <?php $form = ActiveForm::begin(); ?> ...
- Oracle写函数读写日志实例
1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...
- POJ2299 Ultra-QuickSort
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常
#include"stdafx.h" #include"string" #include<iostream> #include<vector& ...
- Ubuntu 12.10安装OpenGL
http://wiki.ubuntu-tw.org/index.php?title=Howto_Install_OpenGL_Development_Environment 前言 OpenGL 是一套 ...
- PHP 7.1 新特性一览
可空类型主要用于参数类型声明和函数返回值声明.主要的两种形式如下:<?phpfunction answer(): ?int { return null; //ok}function ans ...
- [Angularjs]单页应用之分页
写在前面 在项目中,用到单页应用的分页,当时想到使用滚动加载的方案,可是几次尝试都没配置成功,闲着无聊就弄了一个demo. 系列文章 [Angularjs]ng-select和ng-options [ ...
- chrome 阻止跨域操作的解决方法 --disable-web-security
做chrome插件时,遇到https页面上请求htttp页面资源时被blocked的问题,初苦寻解决方法未果,最后找到: 给chrome加上 --disable-web-security 参数
- Windows 下的.NET+ Memcached安装
转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...