1.aspx頁面的頭部
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs" Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %> <%@ Import Namespace="CZBK.ItcastProject.Model" %>
**<%@ Page %>:頁面本身也是一個對象,繼承自Page,而<%@ Page %> 就是設置這個對象的屬性用的。
**Language:C#語言
**AutoEventWireup:啟動頁面事件
**CodeBehind:代碼后置,代碼分離
**Inherits:繼承。 aspx文件會生成一個(子類)類繼承於 aspx.cs文件生成的(父類)類。
**<%@ Import Namespace="CZBK.ItcastProject.Model" %>:引用命名空間
2.aspx 和 aspx.cs
aspx文件會生成一個子類,aspx.cs文件會生成一個父類。aspx繼承於aspx.cs
*在aspx.cs文件中生成一個公共屬性,aspx文件可以訪問到!
*在aspx.cs頁面中使用C#代碼,引用命名空間:<%@ Import Namespace="CZBK.ItcastProject.Model" %>;然后<%= %> 編寫C#代碼,‘=’ 是response.write
UserInfoList.aspx.cs
using CZBK.ItcastProject.Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp.aspx_Demo { public partial class UserInfoList : System.Web.UI.Page { public string StrHtml { get; set; } public List<UserInfo> UserList { get; set; } /// <summary> /// 頁面加載完成以后。Load事件 Form_Load /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { } } }
UserInfoList.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs" Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %> <%@ Import Namespace="CZBK.ItcastProject.Model" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <link href="../Css/tableStyle.css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <div> <table> <tr><th>編號</th><th>用戶名</th><th>密碼</th><th>郵箱</th><th>時間</th><th>刪除</th><th>詳細</th><th>編輯</th></tr> <%=StrHtml%> <% foreach(UserInfo userInfo in UserList){%> <tr> <td><%=userInfo.Id %></td> <td><%=userInfo.UserName %></td> <td><%=userInfo.UserPass %></td> <td><%=userInfo.Email %></td> <td><%=userInfo.RegTime.ToShortDateString() %></td> <td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">刪除</a></td> <td>詳細</td> <td><a href="EditUser.aspx?id=<%=userInfo.Id %>">編輯</a> </td> </tr> <%} %> </table> <hr /> </div> </form> </body> </html>
3.Page_Load 方法
*頁面加載完成觸發(服務端的)
*aspx.cs中的 Page_Load 方法比aspx中的 window.onload = function () {} 方法要先執行!
4.aspx列表展示數據
UserInfoList.aspx:
1.引用命名空間
2.遍歷父類的userInfo(list集合) 公共屬性,循環生成一行tr
3.注意html代碼可以結合C#代碼編寫在aspx中
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.aspx.cs" Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.UserInfoList" %> <%@ Import Namespace="CZBK.ItcastProject.Model" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <link href="../Css/tableStyle.css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <div> <table> <tr><th>編號</th><th>用戶名</th><th>密碼</th><th>郵箱</th><th>時間</th><th>刪除</th><th>詳細</th><th>編輯</th></tr> <% foreach(UserInfo userInfo in UserList){%> <tr> <td><%=userInfo.Id %></td> <td><%=userInfo.UserName %></td> <td><%=userInfo.UserPass %></td> <td><%=userInfo.Email %></td> <td><%=userInfo.RegTime.ToShortDateString() %></td> <td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">刪除</a></td> <td>詳細</td> <td><a href="EditUser.aspx?id=<%=userInfo.Id %>">編輯</a> </td> </tr> <%} %> </table> <hr /> </div> </form> </body> </html>
UserInfoList.aspx.cs
1.定義公共屬性,以便子類訪問
2.調用業務層的方法
using CZBK.ItcastProject.Model; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp.aspx_Demo { public partial class UserInfoList : System.Web.UI.Page { public string StrHtml { get; set; } public List<UserInfo> UserList { get; set; } /// <summary> /// 頁面加載完成以后。Load事件 Form_Load /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); List<UserInfo> list = UserInfoService.GetList(); UserList = list; } } }
5.刪除數據
采用一般處理程序,因為刪除不需要展示復雜的頁面,這里用一般處理程序比aspx方便。
前台的刪除代碼
<td><a href="Delete.ashx?id=<%=userInfo.Id %>" class="deletes">刪除</a></td>
Delete.ashx
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CZBK.ItcastProject.WebApp.aspx_Demo { /// <summary> /// Delete 的摘要說明 /// </summary> public class Delete : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; BLL.UserInfoService UserInfoService = CZBK.ItcastProject.Common.CacheControl.Get<BLL.UserInfoService>(); int id; if(int.TryParse(context.Request.QueryString["id"],out id)) { if (UserInfoService.DeleteUserInfo(id)) { context.Response.Redirect("UserInfoList.aspx"); } else { context.Response.Redirect("Error.html"); } } else { context.Response.Redirect("Error.html"); } } public bool IsReusable { get { return false; } } } }
6.添加數據
1. runat="server" 服務端控件! 會被.netframework解析成客戶端的HTML代碼返回給瀏覽器!但是會自動生成一個隱藏域,name="__ViewState"!這個ViewState用於保持狀態連接,存儲相關數據!
***IsPostBack:是根據__VIEWSTATE隱藏域進行判斷的!
***如果不添加runat="server",則不會自動生成這個隱藏域,name="__ViewState"
2.aspx頁面可以接受Get請求或者Post請求!注意:無論是Get請求還是Post請求,都會訪問aspx.cs類中的Page_Load方法!!
3.aspx頁面區分Get和Post請求:
a.在form表單中添加一個隱藏域,判斷隱藏域的值;aspx.cs類中的Page_load方法中判斷隱藏域的值,如果有值說明表單提交,是Post請求,否則為Get請求
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserInfo.aspx.cs" Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.AddUserInfo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" method="post" action="AddUserInfo.aspx" > <div> 用戶名:<input type="text" name="txtName" /><br /> 密碼:<input type="password" name="txtPwd" /><br /> 郵箱:<input type="text" name="txtMail" /><br /> <input type="hidden" name="isPostBack" value="aaa" /><!--隱藏域的值用於判斷Post請求--> <input type="submit" value="添加用戶" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CZBK.ItcastProject; namespace CZBK.ItcastProject.WebApp.aspx_Demo { public partial class AddUserInfo : System.Web.UI.Page { BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); protected void Page_Load(object sender, EventArgs e) { //判斷 Get,Post請求 //如果隱藏域的值不為空,表示用戶單擊了提交按鈕發出了POST請求 if(!string.IsNullOrEmpty(Request.Form["isPostBack"])) { //post請求 AddUserInfo_Method(); } else { //get請求 } } private void AddUserInfo_Method() { Model.UserInfo userinfo = new Model.UserInfo(); userinfo.UserName = Request.Form["txtName"]; userinfo.UserPass = Request.Form["txtPwd"]; userinfo.Email = Request.Form["txtMail"]; userinfo.RegTime = DateTime.Now; if (UserInfoService.AddUserInfo(userinfo)) { Response.Redirect("UserInfoList.aspx"); } else { Response.Redirect("../Error.html"); } } } }
b.使用服務端的 form表單,runat="server"!可以用IsPostBack來判斷是否為Post請求還是Get請求!!!
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddUserInfo.aspx.cs" Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.AddUserInfo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server" > <div> 用戶名:<input type="text" name="txtName" /><br /> 密碼:<input type="password" name="txtPwd" /><br /> 郵箱:<input type="text" name="txtMail" /><br /> <input type="hidden" name="isPostBack" value="aaa" /><!--隱藏域的值用於判斷Post請求--> <input type="submit" value="添加用戶" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CZBK.ItcastProject; namespace CZBK.ItcastProject.WebApp.aspx_Demo { public partial class AddUserInfo : System.Web.UI.Page { BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); protected void Page_Load(object sender, EventArgs e) { //判斷 Get,Post請求 //IsPostBack:如果是POST請求該屬性的值為true,如果是GET請求該屬性的值為false. //IsPostBack:是根據__VIEWSTATE隱藏域進行判斷的,如果是POST請求那么該隱藏域的值會提交到服務端, //那么IsPostBack屬性也就為true. //如果將form標簽的runat="server"去掉,那么就不能用該屬性進行判斷是POST請求還是GET請求。 //因為去掉form標簽的runat="server",那么就不會再產生 __VIEWSTATE隱藏域了。 if(IsPostBack) { AddUserInfo_Method(); } } private void AddUserInfo_Method() { Model.UserInfo userinfo = new Model.UserInfo(); userinfo.UserName = Request.Form["txtName"]; userinfo.UserPass = Request.Form["txtPwd"]; userinfo.Email = Request.Form["txtMail"]; userinfo.RegTime = DateTime.Now; if (UserInfoService.AddUserInfo(userinfo)) { Response.Redirect("UserInfoList.aspx"); } else { Response.Redirect("../Error.html"); } } } }
7.更新數據
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditUser.aspx.cs" Inherits="CZBK.ItcastProject.WebApp.aspx_Demo.EditUser" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" method="post" action="EditUser.aspx"> <div> 用戶名:<input type="text" name="txtName" value="<%=EditUserInfo.UserName %>" /><br /> 密碼:<input type="text" name="txtPwd" value="<%=EditUserInfo.UserPass %>" /><br /> 郵箱:<input type="text" name="txtMail" value="<%=EditUserInfo.Email %>" /><br /> <input type="hidden" name="txtId" value="<%=EditUserInfo.Id %>" /> <input type="hidden" name="txtRegTime" value="<%=EditUserInfo.RegTime %>" /> <input type="hidden" name="Ispostback" value="aaa" /> <input type="submit" value="編輯用戶" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CZBK.ItcastProject; namespace CZBK.ItcastProject.WebApp.aspx_Demo { public partial class EditUser : System.Web.UI.Page { BLL.UserInfoService userInfoservice = Common.CacheControl.Get<BLL.UserInfoService>(); public Model.UserInfo EditUserInfo { get; set; } protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request.Form["Ispostback"])) { EditMethod();//post update } else { GetUserInfoMethod();//get } } private void GetUserInfoMethod() { int id; if (int.TryParse(Request.QueryString["id"], out id)) { Model.UserInfo userinfo = userInfoservice.GetDeail(id); if (userinfo != null) { EditUserInfo = userinfo; } else Response.Redirect("../Error.html"); } } private void EditMethod() { Model.UserInfo userinfo = new Model.UserInfo(); userinfo.UserName = Request.Form["txtName"]; userinfo.UserPass = Request.Form["txtPwd"]; userinfo.Email = Request.Form["txtMail"]; userinfo.Id = Convert.ToInt32(Request.Form["txtId"]); userinfo.RegTime = DateTime.Now; if (userInfoservice.UpdateUserInfo(userinfo)) { Response.Redirect("UserInfoList.aspx"); } else Response.Redirect("../Error.html"); } } }
