微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Asp.NET笔记八--使用linq+三层架构实现数据的修改

一、在同页面修改个别已知字段的值

  1、DAL层中  

     /// <summary>
        /// 处理缺陷
        /// </summary>
        /// <param name="id">缺陷id</param>
        /// <returns>是否处理成功</returns>
        public bool ChuliDefect(int id)
        {
            //通过id找到缺陷信息
            Defect defect = (from i in db.Defect
                            where i.DefectID == id
                            select i).FirstOrDefault();
            //修改缺陷信息
            defect.defectstate = 2;//2为已处理
            defect.DealTime = DateTime.Now; //处理事件为当前时间
            //将保持提交给数据库
            db.SubmitChanges();
            return true;
        }    

说明:因为修改字段是固定的,所以我们只需要通过id找到要修改的实体字段,即可直接修改数据  

2、BLL层中

 /// <summary>
        /// 处理缺陷
        /// </summary>
        /// <param name="id">缺陷id</param>
        /// <returns>是否处理成功</returns>
        public bool ChuliDefect(int id)
        {
            return dAL.ChuliDefect(id);
        }

  3、UI层中

 前端显示页面

    <asp:Button ID="Button1" runat="server" Text="处理缺陷" Visible='<%#Eval("defectstate").ToString()=="1"?true:false %>' CommandName="ChuLi" CommandArgument='<%#Eval("DefectID") %>' />

 后台RowCommand事件中

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChuLi")
            {
                //获取缺陷ID
                int id = int.Parse( e.CommandArgument.ToString());
                if (bll.ChuliDefect(id))
                {
                    Response.Write("<script>alert('处理成功!')</script>");
           //重新加载GridView实现刷新
            List<DeviceDefect> list = bll.SelectDeviceDefect();

this.GridView1.DataSource = list;
this.GridView1.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('处理失败!')</script>");
                }
            }
        }

-----------------------------------------------------------------------------------------------------------------------------------------------

二、需要跳转页面,由用户输入要修改的值进行修改

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐