程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> ASP.NET ListView控件學習系列2-編輯ListView(Edit,Update,Insert,Delete)

ASP.NET ListView控件學習系列2-編輯ListView(Edit,Update,Insert,Delete)

編輯:關於ASP.NET

一、編輯,取消,更新操作

首先拖一個ListView控件到頁面,然後按如下代碼進行調整,這裡為了利用ListView控件中按鈕的內置功能CommandName必須和我們這裡的名字一樣

前端代碼

<asp:ListView ID="ListView1" runat="server" OnItemEditing="ListView1_ItemEditing"
OnItemCanceling="ListView1_ItemCanceling" OnItemUpdating="ListView1_ItemUpdating">
<ItemTemplate>
<tr>
<td>
<%#Eval("ID") %>
</td>
<td>
<%#Eval("name") %>
</td>
<td>
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:Label ID="IDLable" runat="server" Text='<%#Eval("ID") %>' />
</td>
<td>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
<LayoutTemplate>
<table>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>

這裡我們在App_Code文件夾中添加DataAccess類來模擬數據提供,代碼如下:

模擬數據提供代碼

public class DataAccess
{
public List<Employee> List;
public DataAccess()
{
List = new List<Employee>();
Employee e1 = new Employee {ID=1, Name = "lfm1", Age = 30 };
Employee e2 = new Employee {ID=2, Name = "lfm2", Age = 30 };
Employee e3 = new Employee {ID=3, Name = "lfm3", Age = 30 };
Employee e4 = new Employee {ID=4, Name = "lfm4", Age = 30 };
Employee e5 = new Employee {ID=5, Name = "lfm5", Age = 30 };
Employee e6 = new Employee {ID=6, Name = "lfm6", Age = 30 };
List.Add(e1);
List.Add(e2);
List.Add(e3);
List.Add(e4);
List.Add(e5);
List.Add(e6);
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Sex { get; set; }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved