2.06StuModify.aspx(修改姓名,性别,所在班级)

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:300px;
        margin:50px auto;
    }
    #tbList th,td{
        border:1px solid #0094ff;
        padding:5px;
    }
    </style>
</head>
<body>
    <form method="post" action="06StuModify.aspx">
        <table id="tbList">
            <tr>
                <td>姓名:</td>
                <td><input type="text" id="txtName" name="txtName" value="<%=model.Name %>" /></td>
            </tr>
            <tr>
                <td>所在班级:</td>
                <td><select name="selClass"><%=sbOpts.ToString() %></select></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td>
                    <input type="radio" name="sex" <%=GetChecked(model.Gender,true) %> value=""/><input type="radio" name="sex" <%=GetChecked(model.Gender,false) %> value=""/></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="确定" />
                    <input type="button" id="btnCancel" value="取消" onclick="window.location = '01List.ashx'" />
                </td>
            </tr>
        </table>
        <input type="hidden" name="txtId" value="<%=model.Id %>" />
    </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 WebHelper;

namespace AspxWebForm
{
    public partial class _06StuModify : System.Web.UI.Page
    {
        BLL.Students bllStu = new BLL.Students();
        protected System.Text.StringBuilder sbOpts = new System.Text.StringBuilder();//装 班级下拉框的 html代码
        protected MODEL.Students model = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            //业务一、get过来的时候,根据id展示要修改的数据
            if (Request.HttpMethod.ToLower() == "get")
            {
                ShowData();
            }
            else//业务二、post过来的时候,获取新数据并保存到数据库
            {
                DoModify();
            }
        }

        #region 1.0 生成 班级下拉框 选项 html字符串 +void LoadClassesOption(int stuCId)
        /// <summary>
        /// 生成 班级下拉框 选项 html字符串
        /// </summary>
        /// <param name="stuCId"></param>
        public void LoadClassesOption(int stuCId)
        {
            //创建 班级 业务层对象
            BLL.Classes bllClasss = new BLL.Classes();
            IList<MODEL.Classes> listClass = bllClasss.GetList(false);
            //遍历生成班级下拉框 html
            foreach (MODEL.Classes m in listClass)
            {
                //在生成时候,判断 循环的班级 的id是否 和 参数一样,如果一样,则将这个 option 设置为选中状态 selected
                sbOpts.Append("<option " + (stuCId == m.CID ? "selected" : "") + " value='" + m.CID + "'>" + m.CName + "</option>");
            }
        } 
        #endregion

        #region 2.0 根据 当前学员的性别 和 单选按钮 返回 选中状态 +string GetChecked(string sex, bool isBoy)
        /// <summary>
        /// 根据 当前学员的性别 和 单选按钮 返回 选中状态
        /// </summary>
        /// <param name="sex"></param>
        /// <param name="isBoy"></param>
        /// <returns></returns>
        public string GetChecked(string sex, bool isBoy)
        {
            return isBoy ?
                (sex == "" ? "checked" : "")//如果是 男 单选按钮,则 如果学生是男 则 返回 checked,否则返回 ""
                :
                (sex == "" ? "checked" : "");//如果是 女 单选按钮,则 如果学生是女 则 返回 checked,否则返回 ""
        } 
        #endregion

        #region 3.0展示要修改的数据 -void ShowData()
        /// <summary>
        /// 3.0展示要修改的数据
        /// </summary>
        private void ShowData()
        {
            string strId = Request.QueryString["id"];
            if (CommonHelper.IsNum(strId))
            {
                model = bllStu.GetModel(int.Parse(strId));
                //生成 班级下拉框 选项 html字符串
                LoadClassesOption(model.CId);
            }
            else
            {
                CommonHelper.WriteJs("修改失败~!如果您是美女,请联系管理员~~~qq:111111", "05StuList.aspx");
                Response.End();
            }
        }
        #endregion

        #region 4.0 执行修改 -void DoModify()
        /// <summary>
        /// 执行修改
        /// </summary>
        private void DoModify()
        {
            //验证用户提交的数据
            if (CommonHelper.IsNum(Request.Form["txtId"])
                            && !string.IsNullOrEmpty(Request.Form["sex"].Trim())
                            && CommonHelper.IsNum(Request.Form["selClass"].Trim()))
            {
                //接收提交的数据
                MODEL.Students stuModel = new MODEL.Students();
                stuModel.Id = Convert.ToInt32(Request.Form["txtId"]);
                stuModel.CId = Convert.ToInt32(Request.Form["selClass"]);
                stuModel.Gender = Request.Form["sex"];
                stuModel.Name = Request.Form["txtName"];
                //保存到数据库
                if (bllStu.Update(stuModel))
                {
                    CommonHelper.WriteJs("修改成功~~", "05StuList.aspx");
                }
                else
                {
                    CommonHelper.WriteJs("修改失败~~", "06StuModify.aspx?id=" + Request.Form["txtId"]);
                }
            }
            else
            {
                CommonHelper.WriteJs("您是否恶意修改了参数~~!我报警抓你!", "05StuList.aspx");
            }
            Response.End();//结束输出,不生成前台页面代码
        }
        #endregion
    }
}

   修改业务层BLL.Student

   public bool Update(MODEL.Students model)
        {
            return dal.Update(model) > 0;
        }
修改数据层DAL.Students
 #region 08.修改 +int Update(MODEL.Students model)
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model">数据实体对象</param>
        /// <returns>修改成功的行数</returns>
        public int Update(MODEL.Students model)
        {
            int res = -2;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Students set ");
            strSql.Append("CId=@CId,");
            strSql.Append("Name=@Name,");
            strSql.Append("Gender=@Gender");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Id", SqlDbType.Int,4),
                    new SqlParameter("@CId", SqlDbType.Int,4),
                    new SqlParameter("@Name", SqlDbType.VarChar,50),
                    new SqlParameter("@Gender", SqlDbType.VarChar,1)};
                            parameters[0].Value = model.Id;
                parameters[1].Value = model.CId;
                parameters[2].Value = model.Name;
                parameters[3].Value = model.Gender;

            try
            {
                res = DbHelperSQL.ExcuteNonQuery(strSql.ToString(), parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return res;
        }

 

 

 

 
posted @ 2015-12-30 16:03  狼牙者.net  阅读(246)  评论(0编辑  收藏  举报