.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数据删除(支持批量)的更多相关文章

  1. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...

  2. 《ASP.NET1200例》在DataList里编辑和删除数据

    学习内容:如何创建一个支持编辑和删除数据的DataList.增加编辑和删除功能需要在DataList的ItemTemplate和EditItemTemplate里增加合适的控件,创建对应的事件处理,读 ...

  3. 《ASP.NET1200例》<asp:DataList>分页显示图片

    aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...

  4. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<二>

    ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示 为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己 ...

  5. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>

    分页     在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...

  6. 《ASP.NET1200例》<ItemTemplate>标签在html里面有什么具体的作用

    严格的来说 <ItemTemplate> 在html中无意义,他只是针对诸如 Repeater.DataList.GridView中的一个模板 至于里面的含义,你可以这样想,既然Repea ...

  7. 《ASP.NET1200例》C#在网页上编写动态时钟

    包含Timer类的命名空间有3个 Timer Class (System.Threading)‎ Timer Class (System.Windows.Forms)‎ 一般用于窗体程序 Timer  ...

  8. 《ASP.NET1200例》高亮显示ListView中的数据行并自动切换图片

    aspx <script type="text/javascript"> var oldColor; function SetNewColor(Source) { ol ...

  9. 《ASP.NET1200例》ListView控件之修改,删除与添加

    aspx <body> <form id="form1" runat="server"> <div> <asp:Lis ...

随机推荐

  1. Sping MVC-创建HelloWeb项目(一)

    下面的例子显示怎样使用Spring MVC框架写一个简单的基于Web的应用程序,使用Eclipse IDE作为开发工具,按照下面的步骤使用Spring Web框架来开发一个动态的Web应用 步骤 描述 ...

  2. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  3. BZOJ1816 [Cqoi2010]扑克牌

    Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的 牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1 ...

  4. TYVJ1864 守卫者的挑战

    P1864 [Poetize I]守卫者的挑战 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜 ...

  5. 两个大的整数的运算(java)

    import java.math.BigInteger; public class BigInt { BigInteger m1; BigInteger m2; BigInteger m3; BigI ...

  6. groovy-脚本和类

    在groovy中定义类和java中是一样的.类的方法可以是static,也可以是非static的. groovy中的方法可以是public, protected, private,同时也支持java中 ...

  7. Linux cscope命令

    一.简介 Cscope 是一款开源免费的 C/C++浏览工具,自带一个基于文本的用户界面,通过cscope可以很方便地找到某个函数或变量的定义位置.被调用的位置等信息.Cscope对 C /C++支持 ...

  8. 02 C语言指针

    今天发帖记录自己学习C语言精髓的心理历程,人生就像是一次旅途,沿途总是能看到最美的风景,让我们的思想相逢在C语言中. 一 初识指针,指针的定义 指针是C语言中的一种类型,类似于整形,字符型等.既然C指 ...

  9. Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator

    关于Xcode7 Beta报错 simulator runtime is not available. Unable to open liblaunch_sim.dylib Try reinstall ...

  10. BurpSuite之SQL Injection

    BurpSuite之SQL Injection[平台]:mutillidae[工具]BurpSuite 1.4.07 + FireFox1:安装配置mutillidae如果遇到问题,开下面的帖子.ht ...