码迷,mamicode.com
首页 > Web开发 > 详细

实现办公用品表CRUD(实现全过程)-ASP

时间:2018-09-29 11:20:28      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:query   操作   jin   商业   安全   crud   cut   字符串   i++   

好久都没有写写技术博客了,自己最近几个月都要忙着搬家还有添置家当,所以一些博客就很少去写了,天道酬勤,有些吃饭的家伙还是不能有所懈怠,所以送上一个花了几小时给人事同事写的简单办公用品表的CRUD,希望对正在这条编程这条路上的童鞋们能有所帮助。

  • 1、分析需求。

当我们拿到一个需求或者一个BUG的时候,并不是尽快把自己的十八般武艺全施展出来,或者赶紧动手做,这样后期补漏洞的可能性是大大的,因为你根本没明白自己在做什么,该怎么做,和预防措施。那我们应该怎么做呢?分析需求是很重要的第一步,这个也是我一直教导我徒儿,虽然她不编程,但是每一行的道理是一样的,无论开发也好,实施也好,最重要就是我们开始分析需求,人事同事需要一个办公用品表来进行增删改查,可能这样是很简单的基础操作,但是既然是分析,也要考虑后期的后果,这个使用者的控制,咱们没看到一个表的增删改查就觉得这简单啊,但是当这个使用者是所有人,你能保证数据准确性?还有整个表可能在当前没有很重要,但是咱们的办公用品表对于企业来说是一个重要的一环,后面也会配合ERP和财务的用友软件核算,如果一张办公用品表所有人都能进行篡改,那么只能说你们公司的数据库就是公共资源了,所以我们要和人事同事协商这个使用者,必须是哪几个人,这样责任划分就十分清楚了,当然不能提供批量操作,因为批量后台实现的确特别方便不就是数组么,但是对于数据的安全性而已,这是很大的漏洞,你们可以看看各大型的网站项目,有提供批量操作么?是他们技术不够?那是不可能的,各大型网站的公司必有大牛坐镇,那么都是就安全性考虑,越严谨的逻辑,操作往往会比较复杂,因为不能简单三步点击完事吧,这样被攻击的可能性就一步就能直接侵入你们公司看似机密的机密数据库了。

  • 2、实现需求。

接下来就是众多想要看到高潮了,如何实现需求,第一点,分析需求解决了所有限制和后续麻烦,接下来我们就在这些限制里面实现这些,这才是做对的事情。我用的是ASP+三层实现:

三层分为:Model层(存储映射数据库表)、DAL层(数据访问)、BLL层(业务逻辑实现)

详细解释三层,请百度关键字ASP三层,会有详细解释三层的含义,这是基础需要学习的童鞋要自己去百度了解。

数据库表Office_Supplies展示出来:

技术分享图片

第一咱们先把网站项目新建好,选择网站项目命名为Crud,其次在解决方案中去新建新项选择类库:

Model类库中新建Office_Supplies类,如下:

[Serializable]
    public class Office_Supplies
    {
        int _SuppliesID;
        string _Oneclass;
        string _Twoclass;
        string _Threeclass;
        string _Fourclass;
        string _Unit;
        float _Unitprice;
        int _Store;
        int _State;

        public int SuppliesID
        {
            get { return _SuppliesID; }
            set { _SuppliesID = value; }
        }

        /// <summary>
        /// 一类
        /// </summary>
        public string Oneclass
        {
            get { return _Oneclass; }
            set { _Oneclass = value; }
        }

        /// <summary>
        /// 二类
        /// </summary>
        public string Twoclass
        {
            get { return _Twoclass; }
            set { _Twoclass = value; }
        }

        /// <summary>
        /// 三类
        /// </summary>
        public string Threeclass
        {
            get { return _Threeclass; }
            set { _Threeclass = value; }
        }

        /// <summary>
        /// 四类
        /// </summary>
        public string Fourclass
        {
            get { return _Fourclass; }
            set { _Fourclass = value; }
        }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit
        {
            get { return _Unit; }
            set { _Unit = value; }
        }

        /// <summary>
        /// 单价
        /// </summary>
        public float Unitprice
        {
            get { return _Unitprice; }
            set { _Unitprice = value; }
        }

        /// <summary>
        /// 库存
        /// </summary>
        public int Store
        {
            get { return _Store; }
            set { _Store = value; }
        }

        /// <summary>
        /// 状态
        /// </summary>
        public int State
        {
            get { return _State; }
            set { _State = value; }
        }
    }

DAL类库中新建Office_SuppliesService类,如下:

public class Office_SuppliesService
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="supplies"></param>
        /// <returns></returns>
        public static bool Add(Office_Supplies supplies)
        {
            string sql = string.Format("INSERT INTO dbo.Office_Supplies (一类, 二类, 三类, 四类, 单位, 单价, 库存, 状态) VALUES(‘" + supplies.Oneclass + "‘,‘" + supplies.Twoclass + "‘,‘" +
                supplies.Threeclass + "‘,‘" + supplies.Fourclass + "‘,‘" + supplies.Unit + "‘," + supplies.Unitprice + "," + supplies.Store + "," + supplies.State + ")");
            return DBHelper.ExcuteSQL(sql) > 0 ? true : false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="SuppliesID"></param>
        /// <returns></returns>
        public static bool Delete(int SuppliesID)
        {
            string sql = string.Format("DELETE FROM dbo.Office_Supplies WHERE SuppliesID=" + SuppliesID + "");
            return DBHelper.ExcuteSQL(sql) > 0 ? true : false;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="supplies"></param>
        /// <returns></returns>
        public static bool Modify(Office_Supplies supplies)
        {
            string sql = string.Format("UPDATE dbo.Office_Supplies SET 一类=‘" + supplies.Oneclass + "‘,二类=‘" + supplies.Twoclass + "‘,三类=‘" + supplies.Threeclass + "‘,四类=‘" + supplies.Fourclass +
                "‘,单位=‘" + supplies.Unit + "‘,单价=" + supplies.Unitprice + ",库存=" + supplies.Store + ",状态=" + supplies.State + " where SuppliesID=" + supplies.SuppliesID + "");
            return DBHelper.ExcuteSQL(sql) > 0 ? true : false;
        }

        /// <summary>
        /// 返回所有数据
        /// </summary>
        /// <returns></returns>
        public static List<Office_Supplies> GetAllOffice_Supplies()
        {
            string sql = "SELECT * FROM dbo.Office_Supplies";
            DataTable dt = DBHelper.GetTable(sql);
            List<Office_Supplies> list = new List<Office_Supplies>();
            foreach (DataRow dr in dt.Rows)
            {
                Office_Supplies supplies = new Office_Supplies();
                supplies.SuppliesID = (int)dr["SuppliesID"];
                supplies.Oneclass = dr["一类"].ToString();
                supplies.Twoclass = dr["二类"].ToString();
                supplies.Threeclass = dr["三类"].ToString();
                supplies.Fourclass = dr["四类"].ToString();
                supplies.Unit = dr["单位"].ToString();
                supplies.Unitprice = float.Parse(dr["单价"].ToString());
                supplies.Store = (int)dr["库存"] > 0 ? (int)dr["库存"] : 0;
                supplies.State = (int)dr["状态"] > 0 ? (int)dr["状态"] : 0;
                list.Add(supplies);
            }
            return list;
        }
    }

DAL类库中新建DBHelper类,如下:

 public class DBHelper
    {
        //连接字符串DBHelper
        static string strConn = ConfigurationManager.ConnectionStrings["ecology_ADDLConnectionString"].ToString();

        #region 执行查询,返回DataTable对象
        public static DataTable GetTable(string strSQL)
        {
            return GetTable(strSQL,null);
        }

        public static DataTable GetTable(string strSQL,SqlParameter[] pas)
        {
            return GetTable(strSQL, pas, CommandType.Text);
        }

        /// <summary>
        /// 执行查询,返回DataTable对象
        /// </summary>
        /// <param name="strSQL">sql语句</param>
        /// <param name="pas">参数数组</param>
        /// <param name="cmdtype">Command类型</param>
        /// <returns>DataTable对象</returns>
        public static DataTable GetTable(string strSQL,SqlParameter[] pas,CommandType cmdType)
        {
            DataTable dt = new DataTable();
            using (SqlConnection conn=new SqlConnection(strConn))
            {
                SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
                da.SelectCommand.CommandType = cmdType;
                if (pas!=null)
                {
                    da.SelectCommand.Parameters.AddRange(pas);
                }
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region 执行非查询存储过程和SQL语句
        public static int ExcuteProc(string ProcName)
        {
            return ExcuteProc(ProcName, null);
        }

        public static int ExcuteProc(string ProcName,SqlParameter[] pas)
        {
            return ExcuteSQL(ProcName, pas, CommandType.StoredProcedure);
        }

        public static int ExcuteSQL(string strSQL)
        {
            return ExcuteSQL(strSQL, null);
        }

        public static int ExcuteSQL(string strSQL, SqlParameter[] pas)
        {
            return ExcuteSQL(strSQL, pas, CommandType.Text);
        }

        /// <summary>
        /// 执行非查询存储过程和SQL语句
        /// 增、删、改
        /// </summary>
        /// <param name="strSQL">要执行的SQL语句</param>
        /// <param name="pas">参数列表,没有参数填入null</param>
        /// <param name="cmdType">Command类型</param>
        /// <returns>返回影响行数</returns>
        public static int ExcuteSQL(string strSQL,SqlParameter[] pas,CommandType cmdType)
        {
            int i = 0;
            using (SqlConnection conn=new SqlConnection(strConn))
            {
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                cmd.CommandType = cmdType;
                if (pas!=null)
                {
                    cmd.Parameters.AddRange(pas);
                }
                conn.Open();
                i = cmd.ExecuteNonQuery();
                conn.Close();
            }
            return i;
        }
        #endregion

        #region 查询获取DataReader
        public static SqlDataReader GetReaderByProc(string procName)
        {
            return GetReaderByProc(procName, null);
        }

        public static SqlDataReader GetReaderByProc(string procName, SqlParameter[] paras)
        {
            return GetReader(procName, paras, CommandType.StoredProcedure);
        }

        public static SqlDataReader GetReader(string strSQL)
        {
            return GetReader(strSQL, null);
        }

        public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras)
        {
            return GetReader(strSQL, paras, CommandType.Text);
        }

        public static SqlDataReader GetReader(string strSQL,SqlParameter[] paras,CommandType cmdType)
        {
            SqlDataReader sqldr = null;
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            cmd.CommandType = cmdType;
            if (paras!=null)
            {
                cmd.Parameters.AddRange(paras);
            }
            conn.Open();
            //CommandBehavior.CloseConnection的作用是如果关联的DataReader对象关闭,则连接自动关闭
            sqldr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return sqldr;
        }
        #endregion

        #region 批量插入数据
        /// <summary>
        /// 往数据库中批量插入数据
        /// </summary>
        /// <param name="sourceDt">数据源表</param>
        /// <param name="targetTable">服务器上目标表</param>
        public static void BulkToDB(DataTable sourceDt,string targetTable)
        {
            SqlConnection conn = new SqlConnection(strConn);
            SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);   //用其它源的数据有效批量加载sql server表中
            bulkCopy.DestinationTableName = targetTable;    //服务器上目标表的名称
            bulkCopy.BatchSize = sourceDt.Rows.Count;       //每一批次中的行数

            try
            {
                conn.Open();
                if (sourceDt!=null && sourceDt.Rows.Count !=0)
                {
                    bulkCopy.WriteToServer(sourceDt);      //将提供的数据源中的所有行复制到目标表中
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                if (bulkCopy !=null)
                {
                    bulkCopy.Close();
                }
            }
        }
        #endregion
    }

*注意这里面的ecology_ADDLConnectionString需要去Web.config中去添加一段话:

技术分享图片

 

BLL类库中新建 Office_SuppliesManage类,如下:

public class Office_SuppliesManage
    {
        public static bool Add(Office_Supplies supplies)
        {
            return Office_SuppliesService.Add(supplies);
        }

        public static bool Delete(int SuppliesID)
        {
            return Office_SuppliesService.Delete(SuppliesID);
        }

        public static bool Modify(Office_Supplies supplies)
        {
            return Office_SuppliesService.Modify(supplies);
        }

        public static List<Office_Supplies> GetAllOffice_Supplies()
        {
            return Office_SuppliesService.GetAllOffice_Supplies();
        }
    }

 完成三层后,最重要的是相互引用:

BLL引用DAL、Model,DAL引用Model。

接下来就是数据展现和用户操作的View,在Crud网站项目中新建OfficeSuppliesView.aspx,如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OfficeSuppliesView.aspx.cs" Inherits="Crud.OfficeSuppliesView" %>

<!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>办公用品CRUD</title>
    <style type="text/css">
        #fr_View {
            height: 601px;
            width: 1900px;
        }
        #mainDiv > div {
            float: left;
        }
    </style>
</head>
<body>
    <form id="fr_View" runat="server" style="width:1323px; height:1214px;">
    <div style="height: 1212px; width: 1546px" id="mainDiv">
        <asp:Label runat="server" Text="办公用品表:"  Font-Size="Larger" ForeColor="#006699" Font-Bold="true"></asp:Label><br />
        <asp:GridView ID="gv_View" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
            BorderWidth="1px" PageSize="20" AllowPaging="true" Width="785px" OnRowDeleting="gv_View_RowDeleting" OnRowDataBound="gv_View_RowDataBound" 
            OnRowEditing="gv_View_RowEditing" OnRowCancelingEdit="gv_View_RowCancelingEdit"  OnRowUpdating="gv_View_RowUpdating" OnPageIndexChanging="gv_View_PageIndexChanging" Height="771px" >
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <RowStyle ForeColor="#000066" />
            <Columns>
                <asp:TemplateField HeaderText="SuppliesID">
                    <ItemTemplate>
                        <asp:Label ID="lb_SuppliesID" runat="server" Text=<%# Bind("SuppliesID") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="一类">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Oneclass" runat="server" Text=<%# Bind("Oneclass") %> Height="16px" Width="65px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Oneclass" runat="server" Text=<%# Bind("Oneclass") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="二类">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Twoclass" runat="server" Text=<%# Bind("Twoclass") %> Width="60px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Twoclass" runat="server" Text=<%# Bind("Twoclass") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="三类">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Threeclass" runat="server" Text=<%# Bind("Threeclass") %> Width="65px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Threeclass" runat="server" Text=<%# Bind("Threeclass") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="四类">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Fourclass" runat="server" Text=<%# Bind("Fourclass") %> Width="80px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Fourclass" runat="server" Text=<%# Bind("Fourclass") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="单位">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Unit" runat="server" Text=<%# Bind("Unit") %> Width="30px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Unit" runat="server" Text=<%# Bind("Unit") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="单价">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Unitprice" runat="server" Text=<%# Bind("Unitprice") %> Width="30px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Unitprice" runat="server" Text=<%# Bind("Unitprice") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="库存">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_Store" runat="server" Text=<%# Bind("Store") %> Width="30px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_Store" runat="server" Text=<%# Bind("Store") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="状态">
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_State" runat="server" Text=<%# Bind("State") %> Width="30px"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl_State" runat="server" Text=<%# Bind("State") %>></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="操作" ShowHeader="False">
                    <EditItemTemplate>
                        &nbsp;
                        <asp:Button ID="lbtn_update" runat="server" CausesValidation="True" CommandName="Update"
                            Text="更新" BorderColor="#5cb85c" BackColor="#5cb85c" ForeColor="White"></asp:Button>
                        <asp:Button ID="lbtn_cancel" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="取消" BorderColor="#ff0066" BackColor="#ff0066" ForeColor="White"></asp:Button>
                    </EditItemTemplate>
                    <ItemTemplate>
                        &nbsp;
                        <asp:Button ID="lbtn_edit" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="编辑" BorderColor="#5cb85c" BackColor="#5cb85c" ForeColor="White"></asp:Button>
                        <asp:Button ID="lbtn_delete" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="javascript:return confirm(‘确认要删除么?‘);"  
                            Text="删除" BorderColor="#cf0000" BackColor="#cf0000" ForeColor="White" ></asp:Button>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="true" ForeColor="White" />
            <HeaderStyle BackColor="#006699" Font-Bold="true" ForeColor="White" />
        </asp:GridView>
        <div style="height:300px; width:650px; float:right; border:1px solid #cccccc;">
            <asp:Label runat="server" Text="添加办公用品:" Font-Size="Larger" ForeColor="#ff0066" Font-Bold="true"></asp:Label><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">一类:</label>
            <asp:TextBox ID="tb_oneclass" runat="server"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">二类:</label>
            <asp:TextBox ID="tb_twoclass" runat="server"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">三类:</label>
            <asp:TextBox ID="tb_threeclass" runat="server"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">四类:</label>
            <asp:TextBox ID="tb_fourclass" runat="server"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">单位:</label>
            <asp:TextBox ID="tb_unit" runat="server"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">单价:</label>
            <asp:TextBox ID="tb_unitprice" runat="server" Text="0"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">库存:</label>
            <asp:TextBox ID="tb_store" runat="server" Text="0"></asp:TextBox><br />
            <label style="margin-left:10px;font-size:large;font-weight:bold;color:blue;">状态:</label>
            <asp:TextBox ID="tb_state" runat="server" Text="1"></asp:TextBox>
            <asp:Label runat="server" Text="*注意:状态为0是已借出,状态为1是未借出" Font-Size="Larger" ForeColor="#cc0000" Font-Bold="true"></asp:Label>
            <br /><br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="btn_create" runat="server" Text="添加" OnClick="btn_create_Click" Width="100px" Height="30px" Font-Bold="true" BorderColor="#5cb85c" BackColor="#5cb85c" ForeColor="White"/>
        </div>
    </div>
    </form>
</body>
</html>

View里面涉及到几个事件方法,在

技术分享图片

中去填写:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        protected void Bind()
        {
            gv_View.DataSource = SuppliesBLL.Office_SuppliesManage.GetAllOffice_Supplies();
            gv_View.DataBind();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_View_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int SuppliesID = Convert.ToInt32((gv_View.Rows[e.RowIndex].FindControl("lb_SuppliesID") as Label).Text);
            bool bol = SuppliesBLL.Office_SuppliesManage.Delete(SuppliesID);
            if (bol)
            {
                Bind();
            }
            else
            {
                Response.Write("<script>alert(‘删除失败‘);location.href=OfficeSuppliesView.aspx;</script>");
            }
        }

        protected void gv_View_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int i;
            for (i = 0; i < gv_View.Rows.Count; i++)
            {
                if (e.Row.RowType==DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor=‘#33cccc‘");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }

        /// <summary>
        /// 让当前行处于修改状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_View_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv_View.EditIndex = e.NewEditIndex;
            Bind();
        }

        /// <summary>
        /// 让当前行处于绑定状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_View_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv_View.EditIndex = -1;
            Bind();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_create_Click(object sender, EventArgs e)
        {
            Office_Supplies supplies = new Office_Supplies();
            supplies.Oneclass = this.tb_oneclass.Text.ToString().Trim();
            supplies.Twoclass = this.tb_twoclass.Text.ToString().Trim();
            supplies.Threeclass = this.tb_threeclass.Text.ToString().Trim();
            supplies.Fourclass = this.tb_fourclass.Text.ToString().Trim();
            supplies.Unit = this.tb_unit.Text.ToString().Trim();
            supplies.Unitprice = float.Parse(this.tb_unitprice.Text.ToString().Trim());
            supplies.Store = Convert.ToInt32(this.tb_store.Text.ToString().Trim());
            supplies.State = Convert.ToInt32(this.tb_state.Text.ToString().Trim());
            bool bol = SuppliesBLL.Office_SuppliesManage.Add(supplies);
            if (bol)
            {
                Response.Redirect("OfficeSuppliesView.aspx");
            }
        }

        /// <summary>
        /// 更新至数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_View_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Office_Supplies supplies = new Office_Supplies();
            supplies.SuppliesID= Convert.ToInt32((gv_View.Rows[e.RowIndex].FindControl("lb_SuppliesID") as Label).Text);
            supplies.Oneclass = (gv_View.Rows[e.RowIndex].FindControl("txt_Oneclass") as TextBox).Text.ToString();
            supplies.Twoclass= (gv_View.Rows[e.RowIndex].FindControl("txt_Twoclass") as TextBox).Text.ToString();
            supplies.Threeclass= (gv_View.Rows[e.RowIndex].FindControl("txt_Threeclass") as TextBox).Text.ToString();
            supplies.Fourclass= (gv_View.Rows[e.RowIndex].FindControl("txt_Fourclass") as TextBox).Text.ToString();
            supplies.Unit= (gv_View.Rows[e.RowIndex].FindControl("txt_Unit") as TextBox).Text.ToString();
            supplies.Unitprice = float.Parse((gv_View.Rows[e.RowIndex].FindControl("txt_Unitprice") as TextBox).Text.ToString());
            supplies.Store = Convert.ToInt32((gv_View.Rows[e.RowIndex].FindControl("txt_Store") as TextBox).Text.ToString());
            supplies.State = Convert.ToInt32((gv_View.Rows[e.RowIndex].FindControl("txt_State") as TextBox).Text.ToString());
            bool bol = SuppliesBLL.Office_SuppliesManage.Modify(supplies);
            if (bol)
            {
                Response.Write("<script>alert(‘修改成功‘);</script>");
                gv_View.EditIndex = -1;
                Bind();
            }
            else
            {
                Response.Write("<script>alert(‘修改失败‘);</script>");
            }
        }

        /// <summary>
        /// 翻页
        /// 在GridView当前索引正在更改时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_View_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv_View.PageIndex = e.NewPageIndex;
            Bind();
        }
    

最终效果展现,如下图:

 技术分享图片

点击编辑按钮:

技术分享图片

这样就全过程实现了,办公用品表的CRUD(增删改查),在这里的解释会有些少,如果在实际操作中遇到任何问题,欢迎咨询QQ1165743451,互相探讨学习,在编程之路愈走愈远。

送上我的座右铭:

攀峰之高险岂有崖巅,搏海之明辉何来彼岸,前进不止,奋斗不息!加油加油加油!

 

原著:清风一人醉 http://www.cnblogs.com/W--Jing/

以上方法可以个人分享研究!

不可做商业项目,违者必究!

实现办公用品表CRUD(实现全过程)-ASP

标签:query   操作   jin   商业   安全   crud   cut   字符串   i++   

原文地址:https://www.cnblogs.com/W--Jing/p/9722298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!