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. 彻底解决tap“点透”,提升移动端点击响应速度

    申明!!!最后发现判断有误,各位读读就好,正在研究中.....尼玛水太深了 前言 近期使用tap事件为老夫带来了这样那样的问题,其中一个问题是解决了点透还需要将原来一个个click变为tap,这样的话 ...

  2. javascript的insertBefore、insertAfter和appendChild简单介绍

      target.insertBefore(newChild,existingChild)参数说明:1.target:被添加节点和现有节点的父节点.2.newChild:将要被插入的节点.3.exis ...

  3. hdu_4283_You Are the One(区间DP)

    题目链接:hdu_4283_You Are the One 题意: 有n个人,每个人有个屌丝值,如果果他是第K个上场,不开心指数就为(K-1)*D,然后有个小黑屋,可以调整他们的出场顺序,现在让你调整 ...

  4. Introducing 'bind'

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

  5. npm-link

    https://docs.npmjs.com/cli/link Description Package linking is a two-step process. First, npm link i ...

  6. 一、链接Sql Server2014提示找不到实例的问题解决方案

    在登录数据库时,确认数据库地址.用户名.密码正确的情况下,却报如下错误,则说明目标数据库服务器有相应的服务未启动. 在目标数据库服务器中打开服务列表: 找到SQL Server(****)服务(括号中 ...

  7. 京东商品hover效果

    代码: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&q ...

  8. yii框架中关于控制器中filter过滤器和外部action的使用

    在yii框架中,控制器的过滤器分为执行前和执行后,这里举例是在执行控制器前的过滤. 需要在components/文件夹下定义公共的TestAction.php文件,并且实现run()方法.这个acti ...

  9. LeetCode OJ 4. Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  10. LeetCode OJ 289. Game of Life

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...