管理示例代码

来源:互联网 发布:新田县广电网络 编辑:程序博客网 时间:2024/05/12 07:50

   采用三层代码自动生成,参数形式:

    static int i = 1;//页数

    static int index;//编码号

    static string CNo;//编号

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button2.Visible = false;

            InDate.Text = System.DateTime.Now.ToString("G");
            GridViewBind();

 

            CF.BLL.Goods bll = new CF.BLL.Goods();
            DropDownList1.DataSource = bll.GetAllList().Tables[0].DefaultView;
            DropDownList1.DataBind();

 

            CF.BLL.Admin bll2 = new CF.BLL.Admin();
            DropDownList2.DataSource = bll2.GetAllList().Tables[0].DefaultView;
            DropDownList2.DataBind();
        }
    }

    /// <summary>
    /// 读取数据到GridView1
    /// </summary>
    private void GridViewBind()
    {
        CF.BLL.InWarehouse bll = new CF.BLL.InWarehouse();
        DataSet ds = bll.GetAllList();
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }

 

    /// <summary>
    /// 添加记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
       
            CF.Model.InWarehouse model = new CF.Model.InWarehouse();
            CF.BLL.InWarehouse bll = new CF.BLL.InWarehouse();

            model.InID = bll.GetMaxId();
            model.GoodsID = int.Parse(DropDownList1.SelectedValue);
            model.AdminID = int.Parse(DropDownList2.SelectedValue);
            model.InNumber = int.Parse(InNumber.Text.Trim());
            model.InDate = DateTime.Now;
            model.InQuantity = int.Parse(InQuantity.Text.Trim());
            model.Price = Convert.ToDecimal(Price.Text.Trim());

            int priceTmp = Convert.ToInt32(Price.Text.Trim()) * model.InQuantity;
            model.PriceAmount = Convert.ToDecimal(priceTmp);

            model.Stock = 0;
            model.FHAdminID = -1;
            model.IsInWarehouse = false;
            model.Note = Note.Text.Trim();
            model.InRank = 0;
            model.Place = Place.Text.Trim();

            bll.Add(model);

            Label4.Text = "添加成功!";

            GridViewBind();
            InQuantity.Text = "";
            Price.Text = "";
            InNumber.Text = "";
            Place.Text = "";

    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;

        Label2.Text = "当前页码是" + Convert.ToString(GridView1.PageIndex + i);

        GridViewBind();

    }

 

    /// <summary>
    /// 更新、删除记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "up":
                Button1.Visible = false;
                Button2.Visible = true;
                //获取行编号
                index = Convert.ToInt32(e.CommandArgument);
                //根据行编号,获取整行
                GridViewRow selectedRowu = this.GridView1.Rows[index];
                //获取记录编号
                CNo = selectedRowu.Cells[0].Text;

                CF.BLL.InWarehouse bll = new CF.BLL.InWarehouse();
                CF.Model.InWarehouse model = bll.GetModel(int.Parse(CNo));

                InID.Text = model.InID.ToString();
                DropDownList1.SelectedValue = model.GoodsID.ToString();
                DropDownList2.SelectedValue = model.AdminID.ToString();
                InNumber.Text = model.InNumber.ToString();
                InDate.Text = model.InDate.ToString();
                InQuantity.Text = model.InQuantity.ToString();
                Price.Text = model.Price.ToString();
                Note.Text = model.Note;
                Place.Text = model.Place;
                break;

            case "del":
                //获取行编号
                index = Convert.ToInt32(e.CommandArgument);
                //根据行编号,获取整行
                GridViewRow selectedRowu1 = this.GridView1.Rows[index];
                //获取记录编号
                CNo = selectedRowu1.Cells[0].Text;

                CF.BLL.InWarehouse bll2 = new CF.BLL.InWarehouse();
                bll2.Delete(int.Parse(CNo));
                GridViewBind();

                break;
        }
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[12].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除入库商品:/"" + e.Row.Cells[1].Text.Trim() + "/"吗?')");
            }
        }
    }

 

    /// <summary>
    /// 更新记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        CF.Model.InWarehouse model = new CF.Model.InWarehouse();
        CF.BLL.InWarehouse bll = new CF.BLL.InWarehouse();

        model.InID = int.Parse(InID.Text);
        model.GoodsID = int.Parse(DropDownList1.SelectedValue);
        model.AdminID = int.Parse(DropDownList2.SelectedValue);
        model.InNumber = int.Parse(InNumber.Text.Trim());
        model.InDate = DateTime.Now;
        model.InQuantity = int.Parse(InQuantity.Text.Trim());
        model.Price = Convert.ToDecimal(Price.Text.Trim());

        //int priceTmp = model.Price * int.Parse(InQuantity.Text.Trim());
        model.PriceAmount = model.Price * int.Parse(InQuantity.Text.Trim());
        model.Stock = 0;
        model.FHAdminID = -1;
        model.IsInWarehouse = false;
        model.Note = Note.Text.Trim();
        model.InRank = 0;
        model.Place = Place.Text.Trim();

        bll.Update(model);

        Label4.Text = "更新成功!";
        GridViewBind();
        InQuantity.Text = "";
        Price.Text = "";
        InNumber.Text = "";
        Place.Text = "";

        Button1.Visible = true;
        Button2.Visible = false;


    }