内容:增加,编辑,删除,排序,分页。
1

<%
@ Page Title="" Language="C#" MasterPageFile="~/Mail/MailMasterPage.master" AutoEventWireup="true"2
CodeBehind="MailContacts.aspx.cs" Inherits="Dftr.Web.Mail.MailContacts" %>3
<asp:Content ID="Content1" ContentPlaceHolderID="CPH_Center" runat="server">4
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">5
<ContentTemplate>6
<asp:UpdateProgress ID="UpdateProgress1" runat="server">7
<ProgressTemplate>8
<div class="loading">9
Loading
</div>10
</ProgressTemplate>11
</asp:UpdateProgress>12
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 13
InsertItemPosition="LastItem" 14
ConvertEmptyStringToNull="False" 15
OnItemInserting="ListView1_ItemInserting"16
OnItemCanceling="ListView1_ItemCanceling"17
OnItemDeleting="ListView1_ItemDeleting" 18
OnItemEditing="ListView1_ItemEditing"19
OnItemUpdating="ListView1_ItemUpdating" 20
OnSorting="ListView1_Sorting" 21
OnPagePropertiesChanging="ListView1_PagePropertiesChanging">22
<ItemTemplate>23
<tr style="">24
<td>25
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />26
</td>27
<td>28
<asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />29
</td>30
<td>31
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />32
</td>33
<td>34
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />35
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />36
</td>37
</tr>38
</ItemTemplate>39
<AlternatingItemTemplate>40
<tr style="">41
<td>42
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />43
</td>44
<td>45
<asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />46
</td>47
<td>48
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />49
</td>50
<td>51
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />52
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />53
</td>54
</tr>55
</AlternatingItemTemplate>56
<EmptyDataTemplate>57
<table runat="server" style="">58
<tr>59
<td>60
未返回数据。61
</td>62
</tr>63
</table>64
</EmptyDataTemplate>65
<InsertItemTemplate>66
<tr style="">67
<td>68
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />69
</td>70
<td>71
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />72
</td>73
<td>74
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />75
</td>76
<td>77
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />78
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />79
</td>80
</tr>81
</InsertItemTemplate>82
<LayoutTemplate>83
<table runat="server" style="width: 100%">84
<tr runat="server">85
<td runat="server">86
<table id="itemPlaceholderContainer" runat="server" border="0" style="">87
<tr runat="server" style="">88
<th runat="server">89
<asp:LinkButton ID="SortName" runat="server" Text="姓名" CommandName="Sort" CommandArgument="Name" />90
<asp:Image ID="NameImage" runat="server" Visible="false" />91
</th>92
<th runat="server">93
<asp:LinkButton ID="SortEmail" runat="server" Text="Email" CommandName="Sort" CommandArgument="Email" />94
<asp:Image ID="EmailImage" runat="server" Visible="false" />95
</th>96
<th runat="server">97
<asp:LinkButton ID="SortDescription" runat="server" Text="描述" CommandName="Sort"98
CommandArgument="Description" />99
<asp:Image ID="DescriptionImage" runat="server" Visible="false" />100
</th>101
</tr>102
<tr id="itemPlaceholder" runat="server">103
</tr>104
</table>105
</td>106
</tr>107
</table>108
</LayoutTemplate>109
<EditItemTemplate>110
<tr style="">111
<td>112
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />113
</td>114
<td>115
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />116
</td>117
<td>118
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />119
</td>120
<td>121
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />122
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />123
</td>124
</tr>125
</EditItemTemplate>126
<SelectedItemTemplate>127
<tr style="">128
<td>129
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />130
</td>131
<td>132
<asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />133
</td>134
<td>135
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />136
</td>137
<td>138
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />139
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />140
</td>141
</tr>142
</SelectedItemTemplate>143
</asp:ListView>144
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">145
<Fields>146
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False"147
ShowPreviousPageButton="False" />148
<asp:NumericPagerField />149
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False"150
ShowPreviousPageButton="False" />151
</Fields>152
</asp:DataPager>153
</ContentTemplate>154
</asp:UpdatePanel>155
</asp:Content>156

1
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Web;5
using System.Web.UI;6
using System.Web.UI.WebControls;7
using System.Data;8
using Dftr.Mail;9
using Dftr.Entity.Mail;10

11
namespace Dftr.Web.Mail12


{13
public partial class MailContacts : System.Web.UI.Page14

{15
protected void Page_Load(object sender, EventArgs e)16

{17
if (!IsPostBack)18

{19
ListViewBind();20
}21
}22

23
void ListViewBind()24

{25
DataTable dt = BLLMailContacts.GetList("TestName");26
DataView dv = dt.DefaultView;27
if (ViewState["SortColumn"] != null)28

{29
dv.Sort = ViewState["SortColumn"].ToString() + " " + ViewState["SortDirection"].ToString();30
}31
ListView1.DataSource = dv;32
ListView1.DataBind();33
}34

35
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)36

{37
MailContactsEntity ent = new MailContactsEntity();38
ent.Name = (ListView1.InsertItem.FindControl("NameTextBox") as TextBox).Text;39
ent.Email = (ListView1.InsertItem.FindControl("EmailTextBox") as TextBox).Text;40
ent.Description = (ListView1.InsertItem.FindControl("DescriptionTextBox") as TextBox).Text;41
ent.UserName = "TestName";42

43
if (BLLMailContacts.Add(ent) > 0)44

{45
ListViewBind();46
UpdatePanel1.Update();47
}48
else49

{50
e.Cancel = true;51
}52
}53

54
protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)55

{56
ListView1.EditIndex = e.NewEditIndex;57
ListViewBind();58
}59

60
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)61

{62
MailContactsEntity ent = new MailContactsEntity();63
ent.Id = int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString());64
ent.Name = (ListView1.Items[e.ItemIndex].FindControl("NameTextBox") as TextBox).Text;65
ent.Email = (ListView1.Items[e.ItemIndex].FindControl("EmailTextBox") as TextBox).Text;66
ent.Description = (ListView1.Items[e.ItemIndex].FindControl("DescriptionTextBox") as TextBox).Text;67
ent.UserName = "TestName";68

69
if (BLLMailContacts.Update(ent) > 0)70

{71
ListView1.EditIndex = -1;72
ListViewBind();73
UpdatePanel1.Update();74
}75
else76

{77
e.Cancel = true;78
throw new Exception("未更新");79
}80
}81

82
protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)83

{84
ListView1.EditIndex = -1;85
ListViewBind();86
}87

88
protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)89

{90
if (BLLMailContacts.Remove(int.Parse(ListView1.DataKeys[e.ItemIndex].Value.ToString())) > 0)91

{92
ListViewBind();93
UpdatePanel1.Update();94
}95
else96

{97
e.Cancel = true;98
throw new Exception("未删除");99
}100
}101

102
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)103

{104
//隐藏所有列的图片105
ListView1.FindControl("NameImage").Visible = false;106
ListView1.FindControl("EmailImage").Visible = false;107
ListView1.FindControl("DescriptionImage").Visible = false;108

109

110
ViewState["SortColumn"] = e.SortExpression;111
if (ViewState["SortDirection"] != null)112

{113
if (ViewState["SortDirection"].ToString() == "ASC")114

{115
ViewState["SortDirection"] = "DESC";116

117
(ListView1.FindControl(ViewState["SortColumn"].ToString() + "Image") as Image).ImageUrl = "~/img/arrow_down.gif";118
ListView1.FindControl(ViewState["SortColumn"].ToString() + "Image").Visible = true;119
}120
else121

{122
ViewState["SortDirection"] = "ASC";123

124
(ListView1.FindControl(ViewState["SortColumn"] .ToString()+ "Image") as Image).ImageUrl = "~/img/arrow_top.gif";125
ListView1.FindControl(ViewState["SortColumn"].ToString() + "Image").Visible = true;126
}127
}128
else129

{130
ViewState["SortDirection"] = "ASC";131

132
(ListView1.FindControl(ViewState["SortColumn"].ToString() + "Image") as Image).ImageUrl = "~/img/arrow_top.gif";133
ListView1.FindControl(ViewState["SortColumn"].ToString() + "Image").Visible = true;134
}135
136
ListViewBind();137
UpdatePanel1.Update();138
}139

140
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)141

{142
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);143
ListViewBind();144
UpdatePanel1.Update();145
}146

147
}148
}149

浙公网安备 33010602011771号