05StuList.aspx(学生列表)
05StuList.aspx 加载学生列表(前天代码)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="05StuList.aspx.cs" Inherits="AspxWebForm._05StuList" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>学员列表</title> <style type="text/css"> #tbList { border:1px solid #0094ff; border-collapse:collapse; width:500px; margin:50px auto; } #tbList th,td{ border:1px solid #0094ff; padding:5px; } </style> <script type="text/javascript"> function doDel(id) { if (confirm("您确定要删除吗?")) { window.location = "02Del.ashx?id=" + id; } } </script> </head> <body> <table id="tbList"> <tr> <th>Id</th> <th>姓名</th> <th>班级</th> <th>性别</th> <th>操作</th> </tr> <%=sbHtml.ToString() %> //加载学生信息表 </table> </body> </html>
05StuList.aspx.cs加载学生列表(后台代码)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace AspxWebForm { /// <summary> /// 学员列表 /// </summary> public partial class _05StuList : System.Web.UI.Page { BLL.Students bllStu = new BLL.Students();//创建一个学员对象 protected System.Text.StringBuilder sbHtml = new System.Text.StringBuilder(); protected void Page_Load(object sender, EventArgs e) { //1.查询学员数据 IList<MODEL.Students> list = bllStu.GetList();//获取学生列表 //2.遍历数据 生成 html代码行 foreach (MODEL.Students model in list) { sbHtml.Append("<tr>"); sbHtml.Append("<td>"); sbHtml.Append(model.Id); sbHtml.Append("</td>"); sbHtml.Append("<td>"); sbHtml.Append("<a href='07GetClassDetail.ashx?id=" + model.Id + "'>" + model.Name + "</a>"); sbHtml.Append("</td>"); sbHtml.Append("<td>"); sbHtml.Append(model.CIdMODEL.CName); sbHtml.Append("</td>"); sbHtml.Append("<td>"); sbHtml.Append(model.Gender); sbHtml.Append("</td>"); sbHtml.Append("<td>"); sbHtml.Append("<a href='javascript:doDel(" + model.Id + ")'>删</a> <a href='06StuModify.aspx?id=" + model.Id + "'>改</a>"); sbHtml.Append("</td>"); sbHtml.Append("</tr>"); } } }
BLL业务层
using System; using System.Collections.Generic; using System.Text; namespace BLL { /// <summary> /// 作者: liuhaitao /// 描述: 业务层 -- Students表的业务操作类. /// 时间:2013/2/4 9:35:25 /// </summary> public class Students { private readonly DAL.Students dal = new DAL.Students(); #region 01.根据ID获得实体对象 +MODEL.Students GetModel(int intId) /// <summary> /// 根据ID获得实体对象 /// </summary> /// <param name="intId">id值</param> /// <returns>实体对象</returns> public MODEL.Students GetModel(int intId) { return dal.GetModel(intId); } /// <summary> /// GET DATA LIST /// </summary> public IList<MODEL.Students> GetList() { return dal.GetList(); }
}
}
DAL数据层
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; namespace DAL { /// <summary> /// Author: liuhaitao /// Description: 数据层 -- Students的实体类. /// 创建时间:2013/2/4 9:34:17 /// </summary> public class Students { #region 04.查询数据集合 +IList<MODEL.Students> GetList() /// <summary> /// 查询数据集合 /// </summary> public IList<MODEL.Students> GetList() { return GetListByWhere(""); } #endregion #region 根据where条件查询数据集合 -IList<MODEL.Students> GetListByWhere(string strWhere) /// <summary> /// 根据where条件查询数据集合 /// </summary> private IList<MODEL.Students> GetListByWhere(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select * from Students s inner join Classes c on s.CId=c.Cid where c.CIsDel=0 "); if (strWhere.Trim() != "") { strSql.Append(" and "+strWhere); } DataTable dt = DbHelperSQL.GetDataTable(strSql.ToString()); IList<MODEL.Students> list = null; if (dt.Rows.Count > 0) { list = Table2List(dt); } return list; } #endregion #region a01.将数据表转换成泛型集合接口+ IList<MODEL.Students> Table2List(DataTable dt) /// <summary> /// a01.将数据表转换成泛型集合接口 /// </summary> /// <param name="dt">数据表对象</param> /// <returns>泛型集合接口</returns> public IList<MODEL.Students> Table2List(DataTable dt) { List<MODEL.Students> list = null; if (dt.Rows.Count > 0) { list = new List<MODEL.Students>(); MODEL.Students model = null; foreach (DataRow dr in dt.Rows) { model = new MODEL.Students(); LoadEntityData(model, dr); list.Add(model); } return list; } return null; } #endregion //用来 加载外键实体数据 DAL.Classes dalClass = new Classes(); #region a04.加载实体数据(将行数据装入实体对象中)-void LoadEntityData(MODEL.BlogArticleCate model, DataRow dr) /// <summary> /// 加载实体数据(将行数据装入实体对象中) /// </summary> /// <param name="model">实体对象</param> /// <param name="dr">数据行</param> private void LoadEntityData(MODEL.Students model, DataRow dr) { if (dr.Table.Columns.Contains("Id") && !dr.IsNull("Id")) { model.Id = int.Parse(dr["Id"].ToString()); } if (dr.Table.Columns.Contains("CId") && !dr.IsNull("CId")) { model.CId = int.Parse(dr["CId"].ToString()); //如果行中存在 CName 说明 使用了联合查询,那么就将行里的 属于班级表的数据 填充到 model.CIdModel中! if (dr.Table.Columns.Contains("CName") && !dr.IsNull("CName")) { model.CIdMODEL = new MODEL.Classes();//实例化内外键实体对象 dalClass.LoadEntityData(model.CIdMODEL, dr);//为外键实体装载 数据 } } model.Name = dr["Name"].ToString(); model.Gender = dr["Gender"].ToString(); if (dr.Table.Columns.Contains("IsDel") && !dr.IsNull("IsDel")) { model.IsDel = bool.Parse(dr["IsDel"].ToString()); } if (dr.Table.Columns.Contains("AddTime") && !dr.IsNull("AddTime")) { model.AddTime = DateTime.Parse(dr["AddTime"].ToString()); } } #endregion } }