利用三层架构实现数据的分页显示与页码点击跳转

一、业务需求:

利用三层架构实现对数据库数据的分页功能和点击每个页码实现不同分页面之间的跳转,效果如下图所示:

二、三层结构代码详细示例

1、表现层代码

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo3.aspx.cs" Inherits="Chapter05.Demo3" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <%--引入css样式表--%>
10     <link href="PageBar.css" rel="stylesheet" />
11 </head>
12 <body>
13     <form id="form1" runat="server">
14         <div>
15             <table>
16 
17                 <asp:Repeater ID="Repeater1" runat="server">
18                     <HeaderTemplate>
19                         <tr>
20                             <th>登录名</th>
21                             <th>密码</th>
22                             <th>是否激活</th>                           
23                         </tr>
24                     </HeaderTemplate>
25                     <ItemTemplate>
26                         <tr>
27                             <td><%#Eval("LoginName") %></td>
28                             <td><%#Eval("Pwd") %></td>
29                             <td><%#Eval("IsEnabled") %></td>
30                         </tr>
31                     </ItemTemplate>
32                 </asp:Repeater>
33             </table>
34         <%--    <a href="Demo3.aspx?pageIndex=1">1</a>
35              <a  href="Demo3.aspx?pageIndex=2">2</a>
36              <a>3</a>
37             <a>4</a>
38              <a>5</a>--%>
39             <%--调用后台PageBar类中的代码--%>
40             <%=PageBar %>
41         </div>
42     </form>
43 </body>
44 </html>
View Code

2、表现层后台代码

 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 
 8 using Chapter05.BLL;
 9 using Chapter05.Models;
10 using System.Text;
11 
12 namespace Chapter05
13 {
14     public partial class Demo3 : System.Web.UI.Page
15     {
16         //定义公共属性PageBar
17         public string PageBar { get; set; }
18         protected void Page_Load(object sender, EventArgs e)
19         {
20             if (!IsPostBack)
21             {
22                 //当前页
23                 int pageIndex = 1;
24                 //每页行数
25                 int pageSize = 5;
26                 //总页数
27                 int pageCount = 0;
28                 //总行数
29                 int rowCount = 0;
30                 //判断当前页是否为空
31                 if (Request.QueryString["pageIndex"] != null)
32                 {
33 
34                     pageIndex = Convert.ToInt32(Request.QueryString["pageIndex"].ToString());
35                 }
36 
37                 LoginInfoBLL bll = new LoginInfoBLL();
38                 List<LoginInfo> list = bll.GetPageData(pageIndex, pageSize, ref rowCount, ref pageCount);
39                 //绑定数据源
40                 this.Repeater1.DataSource = list;
41                 this.Repeater1.DataBind();
42                 //调用分页方法PageBar
43                 this.PageBar = Chapter05.PageBar.CreatePageBar(pageIndex, pageCount);
44             }
45 
46         }
47 
48         
49 
50     }
51 }
View Code

3、逻辑判断层代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using Chapter05.DAL;
 8 using Chapter05.Models;
 9 
10 namespace Chapter05.BLL
11 {
12     public class LoginInfoBLL
13     {
14         public DataSet GetAll()
15         {
16             LoginInfoDAL dal = new LoginInfoDAL();
17             return dal.GetAll();
18         }
19 
20         public List<LoginInfo> GetAllList()
21         {
22             LoginInfoDAL dal = new LoginInfoDAL();
23             return dal.GetAllList();
24         }
25 
26         public bool DoDelete(string id)
27         {
28             LoginInfoDAL dal = new LoginInfoDAL();
29             return dal.Delete(id) > 0;
30         }
31 
32         public LoginInfo GetEntityById(string id)
33         {
34             return new LoginInfoDAL().GetEntityById(id);
35         }
36 
37         public bool ModifyLoginInfo(LoginInfo info)
38         {
39             return new LoginInfoDAL().Update(info) > 0;
40         }
41         //存储过程分页
42         public List<LoginInfo> GetPageData(int pageIndex, int pageSize, ref int rowCount, ref int pageCount) {
43 
44             return new LoginInfoDAL().GetPageData(pageIndex, pageSize, ref rowCount, ref pageCount);
45         }
46     }
47 
48 
49 }
View Code

4、数据访问层代码

 ——LoginInfoDAL

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using Chapter05.Models;
  8 using System.Data.SqlClient;
  9 
 10 namespace Chapter05.DAL
 11 {
 12     public class LoginInfoDAL
 13     {
 14 
 15         #region 获取全部数据
 16 
 17         /// <summary>
 18         /// 
 19         /// </summary>
 20         /// <returns></returns>
 21         public DataSet GetAll()
 22         {
 23             //1.定义SQL
 24             string sql = "select * from LoginInfo";
 25             return SqlHelper.GetDataSet(sql, CommandType.Text);
 26         }
 27 
 28         public List<LoginInfo> GetAllList()
 29         {
 30             List<LoginInfo> result = new List<LoginInfo>();
 31             DataSet ds = new DataSet();
 32             ds = GetAll();
 33             if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
 34             {
 35                 foreach (DataRow dr in ds.Tables[0].Rows)
 36                 {
 37                     LoginInfo temp = DataRowToEntity(dr);
 38                     // 转换的结果 ,添加到 集合中
 39                     result.Add(temp);
 40                 }
 41             }
 42             else
 43             {
 44                 result = null;
 45             }
 46             return result;
 47         }
 48 
 49 
 50         #endregion
 51 
 52         #region 查询单个实体
 53 
 54         public LoginInfo GetEntityById(string id)
 55         {
 56             LoginInfo result = null;
 57             string sql = "select * from LoginInfo where id=@id";
 58             SqlParameter param = new SqlParameter("@id", SqlDbType.VarChar, 50);
 59             param.Value = id;
 60 
 61             DataSet ds = SqlHelper.GetDataSet(sql, CommandType.Text, param);
 62             if (ds.Tables[0].Rows.Count > 0)
 63             {
 64                 result = DataRowToEntity(ds.Tables[0].Rows[0]);
 65             }
 66             return result;
 67         }
 68 
 69         #endregion
 70 
 71         #region 分页数据
 72         //定义集合类型的方法,并传入参数,声明输出参数
 73         public List<LoginInfo> GetPageData(int pageIndex, int pageSize, ref int rowCount, ref int pageCount)
 74         {
 75             //定义数据对象集合
 76             List<LoginInfo> result = new List<LoginInfo>();
 77             //调用存储过程
 78             string sql = "PROC_PageData_LoginInfo";
 79 
 80             SqlParameter[] parameters = new SqlParameter[] { 
 81             new SqlParameter("@pageIndex",SqlDbType.Int),
 82             new SqlParameter("@pageSize",SqlDbType.Int),
 83             new SqlParameter("@rowCount",SqlDbType.Int),
 84             new SqlParameter("@pageCount",SqlDbType.Int)
 85             };
 86 
 87             parameters[0].Value = pageIndex;
 88             parameters[1].Value = pageSize;
 89             //声明参数类型:输出参数
 90             parameters[2].Direction = ParameterDirection.Output;
 91             parameters[3].Direction = ParameterDirection.Output;
 92             //调用sqlhelper方法,得到数据集合,命令类型为存储过程
 93             DataSet ds = SqlHelper.GetDataSet(sql, CommandType.StoredProcedure, parameters);
 94             //给输出参数赋值
 95             rowCount = Convert.ToInt32(parameters[2].Value);
 96             pageCount = Convert.ToInt32(parameters[3].Value);
 97             //判断数据集是否为空和数据集合第一张表中是否有数据
 98             if (ds != null && ds.Tables[0].Rows.Count > 0)
 99             {
100                 //遍历第一张表中的每一行数据
101                 foreach (DataRow dr in ds.Tables[0].Rows)
102                 {
103                     //实例化公共模型LoginInfo
104                     LoginInfo temp = new LoginInfo();
105                     //调用方法将每一行数据转换为对象,并赋值给temp对象
106                     temp = DataRowToEntity(dr);
107                     //将temp对象赋给对象集合result
108                     result.Add(temp);
109                 }
110             }
111             else
112             {
113                 result = null;
114             }
115             //返回对象集合
116             return result;
117         }
118 
119         #endregion
120 
121         #region 把数据行转化成  对象
122 
123         private LoginInfo DataRowToEntity(DataRow dr)
124         {
125             LoginInfo temp = new LoginInfo();
126             temp.ID = dr["ID"].ToString();
127             temp.LoginName = dr["LoginName"].ToString();
128             temp.Pwd = dr["Pwd"].ToString();
129             temp.IsEnabled = dr["IsEnabled"].ToString();
130             temp.IsDelete = Convert.ToInt32(dr["IsDelete"]);
131             return temp;
132         }
133 
134         #endregion
135 
136         #region 根据主键删除数据
137         public int Delete(string id)
138         {
139             string sql = "delete from LoginInfo  where id=@id";
140 
141             SqlParameter param = new SqlParameter("@id", SqlDbType.VarChar, 50);
142             param.Value = id;
143             return SqlHelper.ExecuteNonquery(sql, CommandType.Text, param);
144 
145         }
146 
147 
148         #endregion
149 
150         #region 修改数据
151 
152         public int Update(LoginInfo loginInfo)
153         {
154             string sql = "update LoginInfo set LoginName=@loginName,Pwd=@pwd,IsDelete=@isDelete where Id=@id";
155 
156             SqlParameter[] paramters = new SqlParameter[] { 
157                 new SqlParameter("@loginName",SqlDbType.VarChar,50),
158                 new SqlParameter("@pwd",SqlDbType.VarChar,16),
159                 new SqlParameter("@isDelete",SqlDbType.Int),
160                 new SqlParameter("@id",SqlDbType.VarChar,50)
161             };
162 
163             paramters[0].Value = loginInfo.LoginName;
164             paramters[1].Value = loginInfo.Pwd;
165             paramters[2].Value = loginInfo.IsDelete;
166             paramters[3].Value = loginInfo.ID;
167 
168             return SqlHelper.ExecuteNonquery(sql, CommandType.Text, paramters);
169         }
170 
171         #endregion
172     }
173 }
View Code

 ——SQLHelper

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 
 10 namespace Chapter05.DAL
 11 {
 12     public class SqlHelper
 13     {
 14         /// <summary>
 15         /// 获取链接字符串
 16         /// </summary>
 17         private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
 18 
 19         /// <summary>
 20         /// 执行查询操作 
 21         /// </summary>
 22         /// <param name="sql">SQL 语句</param>
 23         /// <param name="type">CommandType 命令的类型【text=>sql 脚本,StoredProcedure=>存储过程】</param>
 24         /// <param name="pars">参数</param>
 25         /// <returns>返回结果集 dataset</returns>
 26         public static DataSet GetDataSet(string sql, CommandType type, params SqlParameter[] pars)
 27         {
 28             //1.创建链接
 29             SqlConnection conn = new SqlConnection(connStr);
 30             //2.打开链接
 31             conn.Open();
 32             //3.创建命令对象
 33             SqlCommand cmd = new SqlCommand(sql, conn);
 34             //4.创建 适配器
 35             SqlDataAdapter da = new SqlDataAdapter(cmd);
 36             // 当传入的参数不为空时,直接添加到Cmd 对象的Parameters 属性上
 37             if (pars != null)
 38             {
 39                 foreach (var item in pars)
 40                 {
 41                     item.Value = item.Value == null ? DBNull.Value : item.Value;
 42                     cmd.Parameters.Add(item);
 43                 }
 44                 //cmd.Parameters.AddRange(pars);
 45             }
 46             //根据 使用者 传入的命令类型,给 Cmd 对象的CommandType 属性赋值;
 47             cmd.CommandType = type;
 48             DataSet ds = new DataSet();
 49             da.Fill(ds);
 50             //关闭链接
 51             conn.Close();
 52             return ds;
 53         }
 54 
 55 
 56         /// <summary>
 57         /// 执行SQL语句:返回 影响行数
 58         /// </summary>
 59         /// <param name="sql">SQL 语句</param>
 60         /// <param name="type">CommandType 命令的类型【text=>sql 脚本,StoredProcedure=>存储过程】</param>
 61         /// <param name="pars">参数</param>
 62         /// <returns></returns>
 63         public static int ExecuteNonquery(string sql, CommandType type, params SqlParameter[] pars)
 64         {
 65             using (SqlConnection conn = new SqlConnection(connStr))
 66             {
 67                 using (SqlCommand cmd = new SqlCommand(sql, conn))
 68                 {
 69                     if (pars != null)
 70                     {
 71                         foreach (var item in pars)
 72                         {
 73                             item.Value = item.Value == null ? DBNull.Value : item.Value;
 74                             cmd.Parameters.Add(item);
 75                         }
 76                         //cmd.Parameters.AddRange(pars);
 77 
 78                     }
 79                     cmd.CommandType = type;
 80                     conn.Open();
 81                     return cmd.ExecuteNonQuery();
 82                 }
 83             }
 84         }
 85 
 86         /// <summary>
 87         /// 执行查询,并返回查询所返回的结果集中第一行的第一列
 88         /// </summary>
 89         /// <param name="sql"></param>
 90         /// <param name="type"></param>
 91         /// <param name="pars"></param>
 92         /// <returns></returns>
 93         public static object ExecuteScalar(string sql, CommandType type, params SqlParameter[] pars)
 94         {
 95             using (SqlConnection conn = new SqlConnection(connStr))
 96             {
 97                 using (SqlCommand cmd = new SqlCommand(sql, conn))
 98                 {
 99                     if (pars != null)
100                     {
101                         //cmd.Parameters.AddRange(pars);
102                         foreach (var item in pars)
103                         {
104                             item.Value = item.Value == null ? DBNull.Value : item.Value;
105                             cmd.Parameters.Add(item);
106                         }
107                     }
108                     cmd.CommandType = type;
109                     conn.Open();
110                     return cmd.ExecuteScalar();
111                 }
112             }
113         }
114     }
115 }
View Code

5、封装的实体类--PageBar代码(实现点击数字切换分页页面)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Web;
 6 
 7 namespace Chapter05
 8 {
 9     public class PageBar
10     {
11         /// <summary>
12         /// 分页方法
13         /// </summary>
14         /// <param name="pageIndex"></param>
15         /// <param name="pageCount"></param>
16         /// <returns></returns>
17         public static string CreatePageBar(int pageIndex, int pageCount)
18         {
19             //计算起始页码
20             int startPage = pageIndex - 5 < 1 ? 1 : pageIndex - 5;
21             // 计算结束页码
22             int endPage = startPage + 9 > pageCount ? pageCount : startPage + 9;
23 
24             StringBuilder sb = new StringBuilder();
25             // 设置Pagebar 的容器
26             sb.Append("<div class='pager'>");
27             // 判断当前页码是否是第一页
28             if (pageIndex != 1)
29             {
30                 //首页
31                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", 1, "首页");
32                 //上一页
33                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", pageIndex - 1, "上一页");
34             }
35             // 循环生成 页码标签
36             for (int i = startPage; i <= endPage; i++)
37             {
38                 //判断 如果页码等于当前页, 设置Current 样式
39                 if (i == pageIndex)
40                 {
41                     sb.AppendFormat("<a class='current' >{0}</a>", i);
42                 }
43                 else
44                 {
45                     sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{0}</a>", i);
46                 }
47             }
48             //判断 当前页,是否是 尾页
49             if (pageIndex != endPage)
50             {
51                 // 下一页
52                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", pageIndex + 1, "下一页");
53                 //尾页
54                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", pageCount, "尾页");
55             }
56             sb.Append("</div>");
57             return sb.ToString();
58         }
59     }
60 }
View Code

6、公共类--Model层代码

    ——封装公共属性

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Chapter05.Models
 8 {
 9     public class LoginInfo
10     {
11         public string ID { get; set; }
12         public string LoginName { get; set; }
13         public string Pwd { get; set; }
14         public string IsEnabled { get; set; }
15         public int IsDelete { get; set; }
16     }
17 }

7、页码条--PageBar的样式表中CSS样式代码

 1 /*PageBarCSS*/
 2 
 3 .pager {
 4     font-size: 12px;
 5     margin: 25px 0;
 6     text-align: center;
 7     color: #2e6ab1;
 8     line-height: 200%;
 9     overflow: hidden;
10 }
11 
12     .pager a {
13         border: 1px solid #9aafe5;
14         color: #2e6ab1;
15         margin: 0 2px;
16         padding: 2px 5px;
17         text-decoration: none;
18     }
19 
20     .pager span.current {
21         background-color: #2e6ab1;
22         border: 1px solid navy;
23         color: #fff;
24         font-weight: bold;
25         margin: 0 2px;
26         padding: 2px 5px;
27     }
28 
29     .pager a.current {
30         background-color: #2e6ab1;
31         border: 1px solid navy;
32         color: #fff;
33         font-weight: bold;
34         margin: 0 2px;
35         padding: 2px 5px;
36     }
View Code

8、SQL数据库中的分页存储过程

 1 use empdb
 2 go
 3 create proc proc_pagedate_empinfor
 4 @pageIndex int,
 5 --声明为float类型的目的:使select @pageCount=CEILING(@rowCount/@pageSize)可以向上取整
 6 @pageSize float,
 7 @rowCount int out,
 8 @pageCount int out
 9 as
10     begin
11     select * from (select ROW_NUMBER() over (order by empId) as Num,* from empinfo) as temp 
12     where Num>(@pageIndex-1)*@pageSize and Num<=@pageIndex*@pageSize
13     select @rowCount=COUNT(1)  from empinfo
14     select @pageCount=CEILING(@rowCount/@pageSize)
15     end
16 go
17 declare @pageCount int,
18         @rowCount int
19 --1:@pageIndex;3:@pageSize
20 exec proc_pagedate_empinfor 1,3,@rowCount out,@pageCount out
21 select @rowCount as 总条数,@pageCount as 总页数
View Code

 

//=================================以下为Reprater控件应用代码示例======================================

 

三、利用Reprater这个显示数据的控件绑定数据源

1、主窗体表现层代码

           ——表现层代码

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo2.aspx.cs" Inherits="Chapter05.Demo2" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9      
10 </head>
11 <body>
12     <form id="form1" runat="server">
13         <div>
14             <table>
15                 <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
16 
17                     <%-- 头部 【表头】 只执行一次 --%>
18                     <HeaderTemplate>
19                         <tr>
20                             <th>登录名</th>
21                             <th>登录密码</th>
22                             <th>是否激活</th>
23                             <th>是否删除</th>
24                         </tr>
25                     </HeaderTemplate>
26                     <%-- 数据项模板 --%>
27                     <ItemTemplate>
28                         <tr>
29                             <td><%#Eval("LoginName") %></td>
30                             <td><%#Eval("Pwd") %></td>
31                             <td><%#Eval("IsEnabled") %></td>
32                             <td><%#Eval("IsDelete") %></td>
33                             <td>
34                                 <asp:Button ID="btnUpate" runat="server" Text="修改" CommandName="btnUpate" 
35                                     CommandArgument='<%#Eval("ID") %>' /></td>
36                             <td>
37                                 <asp:Button ID="btnDelete" runat="server" Text="删除" CommandName="btnDelete" 
38                                     CommandArgument='<%#Eval("ID") %>' /></td>
39                         </tr>
40                     </ItemTemplate>
41                     <SeparatorTemplate>
42                         <tr>
43                             <td colspan="4">
44                                 <hr />
45                             </td>
46                         </tr>
47                     </SeparatorTemplate>
48                     <%-- 交替项 模板 --%>
49                     <AlternatingItemTemplate>
50                         <tr style="background-color: red">
51                             <td><%#Eval("LoginName") %></td>
52                             <td><%#Eval("Pwd") %></td>
53                             <td><%#Eval("IsEnabled") %></td>
54                             <td><%#Eval("IsDelete") %></td>
55                             <td>
56                                 <asp:Button ID="btnUpate" runat="server" Text="修改" CommandName="btnUpate" CommandArgument='<%#Eval("ID") %>' />
57 
58                             </td>
59                             <td>
60                                 <asp:Button ID="btnDelete" runat="server" Text="删除"  CommandName="btnDelete"
61                                     CommandArgument='<%#Eval("ID") %>' 
62                                     />
63 
64                             </td>
65                         </tr>
66                     </AlternatingItemTemplate>
67 
68                     <%-- 尾部模板 只执行一次 --%>
69                     <FooterTemplate>
70                         <tr>
71                             <td colspan="4">这是表尾</td>
72                         </tr>
73                     </FooterTemplate>
74 
75                 </asp:Repeater>
76             </table>
77         </div>
78     </form>
79 </body>
80 </html>
View Code

      ——表现层后台代码

 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 Chapter05.BLL;
 8 using System.Data;
 9 using Chapter05.Models;
10 
11 namespace Chapter05
12 {
13     public partial class Demo2 : System.Web.UI.Page
14     {
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             if (!IsPostBack)
18             {
19                 //this.Repeater1.DataSource = "指定数据源【数据表或者是数据集合】";
20                 //this.Repeater1.DataBind();//才是 绑定数据 到控件
21                 DataSet ds = new DataSet();
22                 ds = new LoginInfoBLL().GetAll();//获取 数据
23 
24                 //this.Repeater1.DataSource = ds.Tables[0];
25                 //this.Repeater1.DataBind();//执行数据的绑定
26                 List<LoginInfo> list = new LoginInfoBLL().GetAllList();
27                 if (list != null)
28                 {
29                     this.Repeater1.DataSource = list;
30                     this.Repeater1.DataBind();
31                 }
32             }
33 
34         }
35 
36         protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
37         {
38             string cmd = e.CommandName;
39             string id = e.CommandArgument.ToString();
40             if (cmd == "btnUpate")
41             {
42                 //修改操作
43                 Response.Redirect("ModifyLoginInfo.aspx?id=" + id);
44             }
45             else
46             {
47                 // 删除操作
48                 LoginInfoBLL bll = new LoginInfoBLL();
49                 bool boo = bll.DoDelete(id);
50                 if (boo)
51                 {
52                     this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "alert('删除成功')", true);
53                     //重新绑定 数据
54 
55                 }
56                 else
57                 {
58                     this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "alert('删除失败')", true);
59                 }
60 
61 
62             }
63         }
64 
65 
66 
67     }
68 }
View Code

2、修改数据的窗体---表现层代码

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ModifyLoginInfo.aspx.cs" Inherits="Chapter05.ModifyLoginInfo" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <script type="text/javascript">
10       
11     </script>
12 </head>
13 <body>
14     <form id="form1" runat="server">
15         <%--<input type="hidden" />--%>
16         <asp:HiddenField ID="hidId" runat="server" />
17         <div>
18             <table>
19                 <tr>
20                     <td>
21                         <asp:Label ID="Label1" runat="server" Text="登录名"></asp:Label></td>
22                     <td>
23                         <asp:TextBox ID="txtLoginName" runat="server"></asp:TextBox></td>
24                 </tr>
25                 <tr>
26                     <td>
27                         <asp:Label ID="Label2" runat="server" Text="密码"></asp:Label></td>
28                     <td>
29                         <asp:TextBox ID="txtPwd" runat="server"></asp:TextBox></td>
30                 </tr>
31                 <tr>
32                     <td>
33                         <asp:Label ID="Label3" runat="server" Text="是否删除"></asp:Label></td>
34                     <td>
35                         <asp:TextBox ID="txtIsDelete" runat="server"></asp:TextBox></td>
36                 </tr>
37                 <tr>
38                     <td colspan="2">
39                         <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" />
40                 
41                         </td>
42                 </tr>
43             </table>
44         </div>
45     </form>
46 </body>
47 </html>
View Code

3、修改数据的窗体---表现层后台代码

 1 using Chapter05.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using Chapter05.BLL;
 9 
10 
11 namespace Chapter05
12 {
13     public partial class ModifyLoginInfo : System.Web.UI.Page
14     {
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             if (!IsPostBack)
18             {
19                 string id = Request.QueryString["id"].ToString();
20                 LoginInfo info = new LoginInfoBLL().GetEntityById(id);
21                 if (info == null)
22                 {
23                     return;
24                 }
25 
26                 this.txtLoginName.Text = info.LoginName;
27                 this.txtPwd.Text = info.Pwd;
28                 this.txtIsDelete.Text = info.IsDelete == 0 ? "正常" : "删除";
29                 this.hidId.Value = info.ID;
30 
31             }
32         }
33 
34         protected void btnSave_Click(object sender, EventArgs e)
35         {
36             LoginInfo temp = new LoginInfo();
37             temp.ID = this.hidId.Value;
38             temp.LoginName = this.txtLoginName.Text.Trim();
39             temp.Pwd = this.txtPwd.Text.Trim();
40             temp.IsDelete = this.txtIsDelete.Text.Trim() == "正常" ? 0 : 1;
41 
42             bool boo = new LoginInfoBLL().ModifyLoginInfo(temp);
43             if (boo)
44             {
45                 //this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "alert('修改成功')",true);
46               
47                // Response.Flush();
48               
49                 Response.Write(@"<script>alert('修改成功');
50                       window.open('Demo2.aspx');
51                     </script>");
52             }
53             else {
54                 this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "alert('修改失败')", true);
55             }
56         }
57     }
58 }
View Code

 

posted @ 2017-09-26 20:18  青红造了个白  阅读(1209)  评论(0编辑  收藏  举报