• asp.net操作GridView添删改查的两种方法 及 光棒效果


         这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧。

    方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新绑定,且删除列要转为模板列): 

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShowCategories();
                ShowProducts();
            }
        }
    
        private void ShowProducts()
        {
            DataTable dt = NorthWind.DBHelp.GetTable("SELECT Product.产品ID, Product.产品名称, Supply.公司名称, Supply.城市, Category.类别名称, Category.图片, Product.单位数量, Product.单价, Product.库存量, Product.中止 FROM Category INNER JOIN Product ON Category.类别ID = Product.类别ID INNER JOIN Supply ON Product.供应商ID = Supply.供应商ID where Product.类别ID="+int.Parse(this.DropDownList1.SelectedValue));
            this.GridView1.DataSource = dt;
            this.GridView1.DataKeyNames = new string[]{"产品ID"};//设置数据操作的主键列
            this.GridView1.DataBind();
        }
    
        private void ShowCategories()
        {
            DataTable dt = NorthWind.DBHelp.GetTable("select * from Category");
            this.DropDownList1.DataSource = dt;
            this.DropDownList1.DataTextField = "类别名称";
            this.DropDownList1.DataValueField = "类别ID";
            this.DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowProducts();
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
    
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //将当前要编辑的行的索引告诉GridView
            this.GridView1.EditIndex = e.NewEditIndex;
            ShowProducts();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //更新数据库
            decimal price = decimal.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text);
            int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
            //更新到数据库
            NorthWind.DBHelp.ExecuteNoQuery("update Product set 单价="+price+" where 产品ID="+productid);
            //重新绑定
            this.GridView1.EditIndex = -1;
            ShowProducts();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            ShowProducts();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //删除数据
            int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
            //更新到数据库
            NorthWind.DBHelp.ExecuteNoQuery("delete from Product where 产品ID=" + productid);
            //重新绑定
            this.GridView1.EditIndex = -1;
            ShowProducts();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //当我们对数据源中的每一行进行数据绑定时触发的事件
            if (e.Row.RowType == DataControlRowType.DataRow)//判断绑定时候该行的状态时数据行还是其他类型的模板
            {
                //添加客户端确认
                LinkButton lnkDelete = (LinkButton)e.Row.Cells[10].FindControl("lnkDelete");
                lnkDelete.Attributes.Add("onclick", "return confirm('你确认删除吗?')");
                //光棒效果
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ff66cc';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
        protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            //获取全选的复选框
            CheckBox chkSelectAll = (CheckBox)sender;
            //获取每一行的复选框
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)this.GridView1.Rows[i].Cells[11].FindControl("chkSelect");
                //修改状态
                chkSelect.Checked = chkSelectAll.Checked;
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //告诉GridView当前显示的数据是第几页的数据
            this.GridView1.PageIndex= e.NewPageIndex;
            ShowProducts();
        }
    
  • 相关阅读:
    go语言:go写的日记簿(开源)
    64位pyqt打包生成的python64位的版本,在win32位下无效
    分享:Notepad++ 6.2.3 发布,开源文本编辑器
    PyQt v4 Python Bindings for Qt v4
    python基于http协议编程:httplib,urllib和urllib2
    XP下IIS5运行MVC站点
    你必须要知道的架构知识~第六章 .NET原生态的ORM软件对比
    从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~七 为DBContext对象应该作为其它实体操作类型的基类,并为它提供统一的提交动作
    MVC最为标准的添加操作(从实体验证到实体入库)
    MVC命名空间中的~UrlHelper中的Action方法告诉我们方法重载的重要性(路由的统一)
  • 原文地址:https://www.cnblogs.com/yingger/p/3283806.html
Copyright © 2020-2023  润新知