一、編輯,取消,更新操作
首先拖一個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; }
}