雪人的英文怎么读:NET三层架构与三层架构下GridView控件增删改操作详解(二)

来源:百度文库 编辑:偶看新闻 时间:2024/05/05 16:15:31

(由于代码过多文章发表不了,所以很多代码用截图表示)

1 新建一个数据库test,新建一个表Users,表中有如下字段(ID,username,password)。其中ID为表示字段。结构如下图:


2 新建三个类库 DiaryBLL(业务逻辑层),DiaryDAL(数据访问层),DiaryModel(业务实体层),将上述三个类库放至解决方案DiaryPro中,然后在新建一个网站DiaryWeb。然后右键单击网站,设为启动项目,结构图如下:


3 添加类库的引用关系

DiaryBLL添加DiaryDAL和DiaryModel,如下图所示:


DiaryDAL添加DiaryModel,如下图所示:



表示层Web添加上述三个,如下图所示:

 

4 类库中类的编写

1)DiaryModel(业务实体层),新建一个User类 代码如下:


该类可以获得User类的各个字段。

2)DiaryDAL(数据访问层)

  由于本层要用到数据库的相关操作,所以这里用到了微软封装的DBHelper.cs类。DBHelper类访问了Web网站的配置文件,获得连接字符串,DiaryDAL要添加引用System.configuration。Web网站的Web.config文件相关代码如下:


(将DBHelper.cs类纺织DiaryDAL目录下,要注意的是DBHelper类下的命名空间要改为DiaryDAL)

 

点击查看DBHelper类

新建一个UserService.cs类,实现增删改查等相关操作,具体代码如下所示:


3)DiaryBLL(业务逻辑层)

 新建一个UserManage.cs类,具体代码如下:


 

4 Web网站的设计

为了实现三层架构下的增删改操作,我们用到了GridView控件,界面如下如所示:


GridView的设计过程如下:

拖放一个GridView控件到页面中,然后添加三个BoundField字段,HeaderText属性分别为用户ID、用户名、密码,绑定字段分别为ID,username,password,一个CommandField字段,显示编辑按钮和删除按钮,然后再将上述四个字段转化为模板列。


Web网站的设计

前台代码如下:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



    无标题页


   


   

        用户列表:

                    BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" PageSize="4" Width="778px" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing"
            OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
           
           
           
               
                   
                       
                   

               

               
                   
                       
                   

                   
                       
                   

               

               
                   
                       
                   

                   
                       
                   

               

               
                   
                                                    Text="更新">
                                                    Text="取消">
                   

                   
                                                    Text="编辑">
                       
                   

               

           

           
           
           
       

   
   

       

       

       

        添加用户

        用户名:
       

        密码:   
       

       
   

 

后台代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DiaryModel;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    protected void Bind()
    {
        GridView1.DataSource = DiaryBLL.UserManage.GetAllUsers();
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Users user = new Users();
        user.UserName = this.tb_username.Text.ToString().Trim();
        user.Password = this.tb_pwd.Text.ToString().Trim();

        bool bol=DiaryBLL.UserManage.Add(user);
        if (bol)
        {
            Response.Redirect("Default.aspx");
        }
       
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int UserID = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label1") as Label).Text);
        bool bol = DiaryBLL.UserManage.Delete(UserID);
        if (bol)
        {
            Bind();
        }
        else
        {
            Response.Write("");
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.FindControl("LinkButton2") as LinkButton;
            if (lb.Text == "删除")
            {
                lb.Attributes.Add("onclick", "return confirm('确认要删除么?');");
            }
        }
    }

    ///


    /// 让当前处于修改状态
    ///

    ///
    ///
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }

    ///


    /// 让当前行处于绑定状态
    ///

    ///
    ///
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }

    ///


    /// 更新至数据库
    ///

    ///
    ///
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Users user = new Users();
        user.UserID = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("Label1") as Label).Text);
        user.UserName = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text.ToString();
        user.Password = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text.ToString();
        bool bol = DiaryBLL.UserManage.Modify(user);
        if (bol)
        {
            Response.Write("");
            GridView1.EditIndex = -1;
            Bind();
        }
        else
        {
            Response.Write("");
        }

    }
}