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 = 1 }); data.Add(new DataEntry { Name = "Ash", Value = 2 }); data.Add(new DataEntry { Name = "Lulu", Value = 3 }); 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 = -1; 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; } } }
时间: 2024-10-10 00:58:22