课程4上课日志3+三层架构
课程4上课日志3一、复习、回顾实例4-2主要知识要点Web窗体、Page类及服务器控件的认识、理解int?、Convert.IsDBNull()的作用、进一步理解实例化一个对象及一个对象为null的含义、进一步理解三层架构项目的开发流程及方法、巩固UI层编辑及美化的基本方法(border-collapse)。Cellspaceing cellpadding Runat=”server”下面二至八为在实例4-2的基础上增加修改密码、注销退出、修改学生信息功能简称为实例4-3二、为实例4-3添加修改密码功能数据访问层、业务逻辑层1、数据访问层实现根据用户名修改密码 public int UpdateUserLogin(string userName, string newPwd) string sql = "update UserLogin set pwd=pwd where UserName=UserName" SqlParameter paras = new SqlParameter new SqlParameter("pwd",newPwd),/new SqlParameter("pwd",SqlDbType.NVarChar), new SqlParameter("UserName",userName)/new SqlParameter("UserName",SqlDbType.NVarChar) ;/paras0.Value = newPwd; /paras1.Value = userName; int count = SqlHelper.ExecuteNonQuery(sql,paras); return count;说明:灰色突出强调代码可以省略。2、业务逻辑层实现调用数据访问层方法修改密码,返回bool类型值public bool UpdateUserLogin(string userName, string newPwd) return dal.UpdateUserLogin(userName,newPwd)>0; 三、为实例4-3添加修改密码功能实现表现层1、设计表现层(1)导航栏添加网站主页、修改密码、注销退出的连接文件(2)UI层添加修改密码窗体文件updatePassWord.aspx,设计布局。可以复制StudentList.aspx、Login.aspx两个文件的前台布局代码,再做修改2、调用业务逻辑层修改密码实现表现层功能UserLoginBll bll=new UserLoginBll();protected void btnSave_Click(object sender, ImageClickEventArgs e) string pwd = txtPwd.Text.Trim(); string newPwd = txtNewPwd.Text.Trim(); string rePwd = txtRePwd.Text.Trim(); if (string.IsNullOrEmpty(pwd) | string.IsNullOrEmpty(newPwd) Response.Write("<script>alert(原始密码和新密码不能为空)</script>"); else if (newPwd != rePwd) Response.Write("<script>alert(两次输入的新密码不一致)</script>"); else UserLogin user = bll.GetUserLogin(Session"UserName".ToString(); if (user != null && user.Pwd = pwd) /执行修改并通过返回值判断是否修改成功 if (bll.UpdateUserLogin(Session"UserName".ToString(), newPwd) Response.Write("<script>alert(密码修改成功)</script>"); else Response.Write("<script>alert(密码修改失败)</script>"); else Response.Write("<script>alert(输入的原始密码不正确或你很久未操作,需要重新登录)</script>"); 3、实现清空功能protected void btnClear_Click(object sender, ImageClickEventArgs e) txtPwd.Text="" txtNewPwd.Text = "" txtRePwd.Text = "" txtPwd.Focus(); 4、测试修改密码功能四、为实例4-3表现层实现验证用户是否登录(Session超时设置)1、验证用户是否登录protected void Page_Load(object sender, EventArgs e) if (Session"UserName" = null) /Session"UserName" = "" Response.Redirect("Login.aspx");/注意与/Login.aspx的区别 思考:(1)/代表根目录,这里Login.aspx前面加与不加/有没有区别(提示:发布网站时,新建虚拟目录时两者对比做测试)。(2)这里未登录或者已超时不能给出提示,最好给出提示,代码修改如下Response.Write("<script>alert(你未登录或已超时,请重新登录);location=Login.aspx</script>");(3)Session"UserName" = ""作用直接启动updatePassWord.aspx时不会报未将对象引用设置到对象的实例,因为直接启动时Session"UserName" = null,这时前台Session"UserName".ToString()就会这样的报错2、修改Session超时时间<system.web> <compilation debug="true" targetFramework="4.0" /><sessionState mode="InProc" cookieless="false" timeout="60"></sessionState></system.web>说明: timeout="60":Session过期时间设置,默认为20分钟。 mode="InProc”:InProc表示将Session存储在进程内,这也是ASP中的存储方式,这是默认值。(mode存储Session的模式/设置将服务器的Session信息存储到哪里,有4种方式,课后查阅资料了解) cookieless="false”:表示使用Cookie模式,这是默认值。(设置客户端的Session信息存储到哪里)五、为实例4-3添加注销退出功能1、通过一般处理程序实现<a href="Logoff.ashx">注销退出</a></div>public void ProcessRequest(HttpContext context) context.Response.ContentType = "text/plain" context.Session"UserName"=null; context.Response.Redirect("/Login.aspx"); 说明:在一般处理程序中使用Session,必须实现IRequiresSessionState 接口,即public class Logoff : IHttpHandler,IRequiresSessionState,此外还得引入接口所在命名空间using System.Web.SessionState;该方法不足:退出系统后通过单击浏览器的后退按钮又回到系统界面(不过此时在其他相关操作页面在Page_Load事件中一定要先判断Session“UserName”是否为空),通过中转页面即可解决此问题。2、通过中转页面<a href="temp.aspx">注销退出</a></div>temp.aspx.csprotected void Page_Load(object sender, EventArgs e) /Session.RemoveAll();Session"UserName" = null; Response.Write("<script>location=Login.aspx</script>"); 六、为实例4-3添加修改学生信息的用户界面可以直接复制updateP