zoukankan      html  css  js  c++  java
  • 关于GridView手动绑定的一段代码,一切尽在不言中

           为GridView绑定主键的方法,在前台的DataGrid标签中加   DataKeyNames="ID"   

    后台获取ID:  int  id=int.parse(this.GridView.DataKeys[e.RowIndex].Value.Tostring());

    如果DataKeyNames绑定了多个列取法:int  id=int.parse(this.GridView.DataKeys[e.RowIndex].Value["ID"].Tostring());

    *****注意GridView在执行添删改时如果是提交性质按钮一定会触发GridView_RowCommand( , )事件

                在此事件下通过e.CommandName属性便可知道是按了添删改的那个按钮

      下面是GridView实现全选和取消全选的功能实现:

                 为GridView1添加一个模板列,给其头模板添<HeaderTemplate>加一个Html控件CheckBox,添加

       onClick事件代码如下

          <HeaderTemplate>
           <input id="chkSelectAll" type="checkbox" value="全选" onclick="SelectAll(this)" />
         </HeaderTemplate>

             给其项模板<ItemTemplate>添加服务端控件CheckBox。在页面的头标签<head>中添加如下JS方法实现:  

            <head runat="server">
              <title>无标题页</title>
              <script type="text/javascript">
                 function SelectAll(oCheck)
                  {
                     var oTable = document.getElementById("GridView1");
                     if (oTable)
                      {
                         var oInputs = oTable.getElementsByTagName("input");
                         for (var i = 0; i < oInputs.length; i++)
                          {
                              if (oInputs[i].type == "checkbox")
                                {
                                   oInputs[i].checked = oCheck.checked;
                                }
                          }
                      }
                  }
               </script>
             </head>

     

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    public partial class GridViewDemo_Default3 : System.Web.UI.Page

    {

        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)//判断绑定时候该行的状态时数据行还是其他类型的模板

            {

            //如果要把ID列绑到GridView中为连续不断的ID,可先添加绑定列ID,取消其DataFiled的绑定,在该事件加下面代码:

                  //e.Row.Cells[0].Text=(e.Row.RowIndex+1).ToString();

                //添加客户端确认

                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';this.style.cursor='pointer';");

                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;this.style.cursor='default';");

            }

        }

        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();

        }

    }

     
  • 相关阅读:
    [C]recursion递归计算阶乘
    [Python]reduce function & lambda function & factorial
    [C/JAVA] ceil, floor
    OC项目调用C++
    Xcode 代码注释
    百度云加速器
    UITableView和MJReFresh结合使用问题记录
    OC 类的load方法
    JLRoutes笔记
    推送通知项目记录
  • 原文地址:https://www.cnblogs.com/yingger/p/2462454.html
Copyright © 2011-2022 走看看