ListView的一些事件(插入、删除、编辑、读取)

利用ListView做的部门管理

一、页面代码

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="department.aspx.cs" Inherits="Web.systemset.department" %>

<!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>
<link href="../style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lbMessage" runat="server" ForeColor="Red"></asp:Label>
<br />
<asp:ListView ID="lvDepartment" runat="server" DataKeyNames="F_DepartmentID" EnableModelValidation="True"
InsertItemPosition="LastItem" oniteminserting="lvDepartment_ItemInserting"
onitemediting="lvDepartment_ItemEditing"
onitemcanceling="lvDepartment_ItemCanceling"
onitemdeleting="lvDepartment_ItemDeleting"
onitemupdating="lvDepartment_ItemUpdating">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" onclientclick="return confirm('确认删除?')"/>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
</td>
<td>
<asp:Label ID="F_DepartmentNameLabel" runat="server"
Text='<%# Eval("F_DepartmentName") %>' />
</td>
<td>
<asp:Label ID="F_OrderLabel" runat="server" Text='<%# Eval("F_Order") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
</td>
<td>
<asp:TextBox ID="F_DepartmentNameTextBox" runat="server" Text='<%# Bind("F_DepartmentName") %>' CssClass="wenben" />
</td>
<td>
<asp:TextBox ID="F_OrderTextBox" runat="server" Text='<%# Bind("F_Order") %>' CssClass="order"
onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;" />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>未返回数据。</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
</td>
<td>
<asp:TextBox ID="F_DepartmentNameTextBox" runat="server" Text='<%# Bind("F_DepartmentName") %>' CssClass="wenben" />
</td>
<td>
<asp:TextBox ID="F_OrderTextBox" runat="server" Text='<%# Bind("F_Order") %>' CssClass="order"
onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;" />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" onclientclick="return confirm('确认删除?')" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
</td>
<td>
<asp:Label ID="F_DepartmentNameLabel" runat="server" Text='<%# Eval("F_DepartmentName") %>' />
</td>
<td>
<asp:Label ID="F_OrderLabel" runat="server" Text='<%# Eval("F_Order") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<td runat="server" align="left"></td>
<td runat="server" align="left"><strong>部门名称</strong></td>
<td runat="server" align="left"><strong>顺序</strong></td>
</tr>
<tr ID="itemPlaceholder" runat="server" align="left">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
ShowNextPageButton="False" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
</td>
<td>
<asp:Label ID="F_DepartmentNameLabel" runat="server" Text='<%# Eval("F_DepartmentName") %>' />
</td>
<td>
<asp:Label ID="F_OrderLabel" runat="server" Text='<%# Eval("F_Order") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
</form>
</body>
</html>

二、Page_Load页面加载事件

View Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack&&configcompany!=null)
{
lvDepartmentBind();
}
}

三、lvDepartment_ItemInserting插入事件

View Code
protected void lvDepartment_ItemInserting(object sender, ListViewInsertEventArgs e)
{
TextBox txDepart = (TextBox)e.Item.FindControl("F_DepartmentNameTextBox");
TextBox txOrder = (TextBox)e.Item.FindControl("F_OrderTextBox");
if (txDepart.Text == "")
{
lbMessage.Text = "部门不能为空";
}
else if (txOrder.Text == "")
{
lbMessage.Text = "没有填写顺序";
}
else
{
bool bl = Mis.GetUserInfo.InsertDepartment(txDepart.Text, Convert.ToInt32(txOrder.Text), configcompany.CompanyID);
lvDepartmentBind();
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
}
}

四、lvDepartment_ItemEditing编辑事件

View Code
protected void lvDepartment_ItemEditing(object sender, ListViewEditEventArgs e)
{
lvDepartment.EditIndex = e.NewEditIndex;
lvDepartmentBind();
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
}

五、lvDepartment_ItemDeleting删除事件

View Code
protected void lvDepartment_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
int KeyId = Convert.ToInt32(lvDepartment.DataKeys[e.ItemIndex].Value);
bool bl = Mis.GetUserInfo.DeleteDepartment(KeyId);
lvDepartmentBind();
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
}

六、lvDepartment_ItemUpdating更新事件

View Code
protected void lvDepartment_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
int KeyId = Convert.ToInt32(lvDepartment.DataKeys[e.ItemIndex].Value);
TextBox txDepart =(TextBox)lvDepartment.Items[e.ItemIndex].FindControl("F_DepartmentNameTextBox");
TextBox txOrder =(TextBox)lvDepartment.Items[e.ItemIndex].FindControl("F_OrderTextBox");
if (txDepart.Text=="")
lbMessage.Text ="部门不能为空";
else if(txOrder.Text=="")
lbMessage.Text="顺序不能为空";
else
{
string depart =Server.HtmlEncode(txDepart.Text);
int order = Convert.ToInt32(txOrder.Text);
bool bl = Mis.GetUserInfo.UpdateDepartment(depart,order,KeyId);
lvDepartment.EditIndex = -1;
lvDepartmentBind();
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
}
}

七、lvDepartment_ItemCanceling取消事件

View Code
protected void lvDepartment_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
//取消编辑
if (e.CancelMode == ListViewCancelMode.CancelingEdit)
{
//e.Cancel = true;
lvDepartment.EditIndex = -1;
lvDepartmentBind();
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
}
else if (e.CancelMode == ListViewCancelMode.CancelingInsert)
{
lvDepartmentBind();
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
return;
}
}

八、数据绑定

View Code
posted @ 2011-12-09 11:48  做个好男人  阅读(4008)  评论(1编辑  收藏  举报