DataKeys属性 GridView有一个DataKeys属性会经常用到,这里单独讲一下。比如:有一个删除按钮列,点击删除后,要删除当前行,但是你怎么从后台程序中获取到当前行的关键字段的值呢?方法有多种,但最可靠的方法还是通过GridView的DataKeys属性。下面程序是示范: 1.在进行数据绑定之时,设置DataKeyNames属性的值: this.GridView1.DataSource = ds.Tables[0]; this.GridView1.DataKeyNames = new string[] { "CustomerID", "CompanyName" }; this.GridView1.DataBind(); 此行程序的作用是将CustomerID和CompanyName两个字段的值放入到DataKeys数组中。
2.在GridView1_RowDeleting事件中写如下程序: string id = this.GridView1.DataKeys[e.RowIndex]["CustomerID"].ToString(); string name = this.GridView1.DataKeys[e.RowIndex]["CompanyName"].ToString(); 这样就获取到了当前行记录在DataKeys数组中的值。
gridview中超连接的字段控制字符串的长度 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HyperLink temp = (HyperLink)e.Row.Cells[1].Controls[0]; temp.Text = temp.Text.Length > 33 ? temp.Text.Substring(0, 32) + "..." : temp.Text; } }


938

被折叠的 条评论
为什么被折叠?



