本系列文章将通过一个简单的实例,结合我自己使用ListView的情况,展示如何用ASP.NET 3.5 ListView控件进行基本的Insert、Edit和Delete操作。

系统要求:

  • Windows XP SP2 or higher
  • VS2008 Beta 2 or Visual Web Developer 2008 Express Edition Beta 2

在本系列的第一篇中,介绍了如何实现ListView的Insert功能。本篇介绍如何实现Edit功能。

实现Edit功能

在ListView中加入Edit功能与Insert的方式非常相似。我们需要添加一个EditItemTemplate,在里边放置两个TextBox用于接收Name和Value的用户输入,以及一个Save按钮。这个Save按钮的CommandName设置为Update:

<EditItemTemplate>
<li>Name: <asp:TextBox runat="server" ID="Name" Text='<%# Bind("Name") %>' />
Value: <asp:TextBox runat="server" ID="Value" Text='<%# Bind("Value") %>' /> <br /> <asp:Button runat="server" ID="Update" Text="Save" CommandName="Update" />
</li>
</EditItemTemplate>

同时,为ItemTemplate加上一个Edit按钮,其CommandName设置为Edit:

<ItemTemplate>
<li><%# Eval("Name") %>(#<%# Eval("Value") %>)<br /> <asp:Button runat="server" ID="Edit" CommandName="Edit" Text="Edit" />
</li><br />
</ItemTemplate>

然后为ListView添加ItemUpdating事件的处理方法。在这个方法里,我们如果得到当前修改的数据对象?我用到过的方法有三个:

  • 为Save按钮添加CommandArgument属性,其值设为当前数据对象的Value值;不过,在ItemUpdating里并不能直接取得CommandArgument值,所以这种方法的处理代码,应该挪至ItemCommand事件的处理方法中;
  • 在EditItemTemplate中放置一个Literal,其Text属性设置为数据对象的Value值,并将该Literal隐藏。在ItemUpdating中,对当前Item使用FindControl方法取得该Literal,进而取得Value值;
  • 使用ListView的DataKeyNames属性;

我们将使用第3种方法。为ListView添加DataKeyNames属性,并将其值设为Value。这样,在ListView被数据填充时,ListView的DataKeys属性会被自动填充上每个数据对象的Value值。

在ItemUpdating处理方法中,我们以事件参数ListViewUpdateEventArgs的ItemIndex值为下标,从ListView的DataKeys中取得当前编辑的数据对象的Value值,然后进行保存操作,最后,将ListView的EditIndex设为-1以退出Edit状态:

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
int value = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());
List<DataEntry> data = Data;
DataEntry entry = data.Single(d => d.Value == value);
entry.Name = (ListView1.Items[e.ItemIndex].FindControl("Name") as TextBox).Text;
entry.Value = int.Parse((ListView1.Items[e.ItemIndex].FindControl("Value") as TextBox).Text);
Data = data;
ListView1.EditIndex = -;
BindData();
}

事俱备,为ListView的ItemEditing事件添加处理方法,并在其中设置ListView的EditIndex属性以进入Edit状态,EditIndex值可以从事件参数ListViewEditEventArgs的NewEditIndex中获得:

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
BindData();
}

运行示例网站,测试Edit功能,貌似正常。贴图就略了:)) 注:我们假定数据对象的Value值是唯一(相当于主健)的,为了不引入过多的细节,添加、修改数据时不作对该值的唯一性检查。

@ashchan

ListView控件的Insert、Edit和Delete功能(第二部分)的更多相关文章

  1. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

  2. ListView控件的Insert、Edit和Delete功能(第一部分)

    摘自:http://blog.ashchan.com/archive/2007/08/28/listview-control-insert-edit-amp-delete-part-1aspx/ Li ...

  3. ListView控件的Insert、Edit和Delete功能第三部分(自我总结)

    1.刚开始在第一部分显示数据的时候出现如下错误: 修改: @Page 中的EnableEventValidation="false" 2.点击各个按钮没有反应. 修改:为page_ ...

  4. listview控件专题

    listview控件加CheckBox 窗口load: listViewLayersName.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderS ...

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

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

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

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

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

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

  8. SQLite数据库、ListView控件的使用

    android下数据库的创建(重点) 在Android平台上,集成了一个轻量级嵌入式关系型数据库—SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本 ...

  9. ListView控件绑定DataSet

    DataSet数据集,数据缓存在客户端内存中,支持断开式连接.   在对DataSet做操作的时候,首先一定要修改其行的状态,然后执行SqlDataAdapter的Update方法,Update方法根 ...

随机推荐

  1. Introducing 'bind'

    原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-bind/ 上一篇讨论了如何理解let作为一个能实现contin ...

  2. Markdown 基础

    How to use Markdown H1 text. H2 text. H3 text. H4 text. H5 text. H6 text. Text This is italic text. ...

  3. vs2012常用快捷键总结

    ctrl+k+c  注释 ctrl+k+u 取消注释 ctrl+m+t 折叠代码 ctrl+t 取消折叠 ctrl+k+d 调节代码 ctrl+f 查找 ctrl+g  跳转行(一般调试的时候显示哪行 ...

  4. idea代码调试debug篇

    主要看图,看图一目了然. 断点的设定和eclipse一样,只要点一下就可以,下面是我设定的几个断点,再下面的三个窗口是用来调试代码的,这个和eclipse类似 调试常用的快捷键 F9          ...

  5. LightOJ 1337 F - The Crystal Maze (bfs)

    Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...

  6. There was a problem parsing the package(android)

    android phone when you install the application there will inevitably be "a problem parsing the ...

  7. Program Files 与Program Files (x86)

    在64位系统下,为了更好的兼容32位程序,在安装一些32位程序(注意某些程序他就是32位的),会默认扔到program files(x86)这个文件夹下,而一些64位的程序,或程序本身没有位数区别的, ...

  8. jquery删除未来项 jquery on

    $(document).on('click', '.delbtn', function() { if (confirm("确定要删除吗?")) { var adminid=$(th ...

  9. IOS应用的国际化

    IOS应用的国际化,主要分为3个部分: Info.plist 文件的国际化 .xib文件的国际化 .m文件的国际化 下面我就来一一说明. Info.plist 文件的国际化我们使用Xcode 4.5 ...

  10. bootstrap开始咯

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...