1.刚开始在第一部分显示数据的时候出现如下错误:

修改:

@Page 中的EnableEventValidation="false"

2.点击各个按钮没有反应。

修改:为page_load事件加判断是否回发。if (!Page.IsPostBack)

3.ItemPlaceholderID和<asp:PlaceHolder ID="Layout" runat="server"></asp:PlaceHolder>

3. ListViewInsertEventArgs e    中可以通过 var temp = e.Values;可以通过键值来得到value的值。

源代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NX.Manage.WebForm1" EnableEventValidation="false" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:listview ID="ListView1" runat="server" ItemPlaceholderID="Layout"
onitemcommand="ListView1_ItemCommand"
oniteminserting="ListView1_ItemInserting"
onitemupdating="ListView1_ItemUpdating" DataKeyNames="Value"
onitemediting="ListView1_ItemEditing">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="Layout" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><%# Eval("Name") %> (#<%# Eval("Value") %>)</li><asp:Button runat="server" ID="Create" CommandName="Create" Text="New" />
<asp:Button runat="server" ID="Edit" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EmptyDataTemplate>
<asp:Button runat="server" ID="Create" CommandName="Create" Text="New" />
</EmptyDataTemplate>
<InsertItemTemplate>
<li>Name: <asp:TextBox runat="server" ID="NewName" Text='<%# Bind("Name") %>' /><br />
Value: <asp:TextBox runat="server" ID="NewValue" Text='<%# Bind("Value") %>' />
<asp:Button runat="server" ID="Insert" Text="Save" CommandName="Insert" /> </li><br />
</InsertItemTemplate>
<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>
</asp:listview>
</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; namespace NX.Manage
{
public partial class WebForm1 : System.Web.UI.Page
{
static readonly string SESSION_KEY = "_sk_dataentry_";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ListView1.DataSource = Data;
ListView1.DataBind();
}
}
protected List<DataEntry> Data
{
get
{
List<DataEntry> data = Session[SESSION_KEY] as List<DataEntry>;
if (data == null)
{
data = new List<DataEntry>();
data.Add(new DataEntry { Name = "James", Value = });
data.Add(new DataEntry { Name = "Ash", Value = });
data.Add(new DataEntry { Name = "Lulu", Value = });
ViewState[SESSION_KEY] = data;
} return data;
}
set { Session[SESSION_KEY] = value; }
} protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Create")
{
ListView1.InsertItemPosition = InsertItemPosition.LastItem;
BindData();
}
} private void BindData()
{
ListView1.DataSource = Data;
ListView1.DataBind();
} protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
var temp = e.Values;
ListViewItem item = e.Item;
try
{
string name = (item.FindControl("NewName") as TextBox).Text;
int value = int.Parse((item.FindControl("NewValue") as TextBox).Text);
List<DataEntry> data = Data;
data.Add(new DataEntry { Name = name, Value = value });
Data = data; ListView1.InsertItemPosition = InsertItemPosition.None; BindData();
}
catch { }
} protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
var c= e.OldValues;
var f= e.NewValues;
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(); } protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
BindData();
} }
[Serializable]
public class DataEntry
{
public string Name { get; set; }
public int Value { get; set; } }
}

ListView控件的Insert、Edit和Delete功能第三部分(自我总结)的更多相关文章

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

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

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

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

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

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

  4. Windows程序==>>使用ListView控件展示数据

    使用ListView控件展示数据 01.ImageList控件 1.了解了解         属性 说明 Images 储存在图像列表中的所有图像 ImageSize 图像列表中图像的大小 Trans ...

  5. listview控件专题

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

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

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

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

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

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

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

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

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

随机推荐

  1. team viewer - rollback framework could not be initialized

    rollback framework could not be initialized, 在安装team viewer 的时候出现的这个错误信息,求大师帮忙 https://zhidao.baidu. ...

  2. Redis断线重连编码注意事项

    应用在Redis重启.网络闪断并恢复正常后,应用必须能够自恢复,下面以Java语言的jedis客户端为例说明: 1.作为发布者 Jedis对象不能作为单例,网络闪断后该Jedis对象无法自恢复.应该每 ...

  3. iOS开发打电话的功能

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 NSMutableString * phoneStr=[[NSMutableString alloc] init ...

  4. Eclipse版本区别

    各版本的区别: Eclipse IDE for Java Developers 是Eclipse的platform加上JDT插件,用来java开发的 Eclipse IDE for Java EE D ...

  5. 做一个视频播放器在没开始播放的时候有一张图片实际上就是拿一张图片盖住视频承载的屏幕当出发。play的时候图片隐藏 img

    saxda 某个元素.style.class='';也可以是.className <!DOCTYPE html><html lang="en"><he ...

  6. 二维离散平稳小波分解swt2

    对信号X进行N尺度平稳小波分解 [A,H,V,D]=swt2(X,N,'wname'); clc,clear all,close all; load woman; [cA,cH,cV,cD]=swt2 ...

  7. SOJ 1210 二叉树

    1210. 二叉树 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description 在众多的数据结构中,二叉树是一种特殊而重要的结构,有 ...

  8. Spring入门学习(一)

    Spring的主要功能是控制反转和面向切面编程,下面我们就来编写第一个spring的程序来体验一下控制反转 首先是加载配置文件 <?xml version="1.0" enc ...

  9. ckeditor 基础

    <!DOCTYPE html> <!-- Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights rese ...

  10. SharePoint 2013 配置InfoPath 列表表单

    转载来源:http://www.cnblogs.com/jianyus/p/3470113.html SharePoint列表,都是通过表单展示,有时候不太符合要求,这时候,我们可以通过定制表单,来是 ...