今天看到一篇关于GridView使用的文章感觉很好,特来分享,关于向服务器添加事件
1、删除前加判断
protected void gv_productJingPing_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if(e.Row.RowType == DataControlRowType.DataRow)
   {
        e.Row.Cells[4].Attributes.Add(" "javascript:return confirm('您真的要删除["+e.Row.Cells[1].Text+"]吗?')");
   }
}
 
2、编辑时控制文本框的宽度
protected void gv_productJingPing_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
         if ((e.Row.RowState & DataControlRowState.Edit) != 0)
         {
              TextBox txtUrl = (TextBox)e.Row.Cells[2].Controls[0];
              txtUrl.Width = 230;
         }
     }
}
(说明:如果使用模板列,则可自定义列长)

3、显示图片
<asp:ImageField DataImageUrlField="p_w_picpathpath" DataImageUrlFormatString="img/{0}">
</asp:ImageField>
说明:p_w_picpathpath指的是数据库图片字段名,img指图片存放位置

4 、 GridView的双击/单击/键盘按键/鼠标悬浮/移出等事件
<script language="javascript">       
        function DbClickEvent(d)
        {
              window.alert("事件类型: DoubleClidk  作用对象: " + d);           
        }
        function ClickEvent(d)
        {
              window.alert("事件类型: OnClick  作用对象: " + d);           
        }
        function GridViewItemKeyDownEvent(d)
        {
              window.alert("事件类型: GridViewItemKeyDownEvent  作用对象: " + d);      
        }         
</script>
         (绑定事件)
if( e.Row.RowType == DataControlRowType.DataRow)
{
         //鼠标移动到每项时颜色交替效果
         e.Row.Attributes.Add ("OnMouseOut",  "this.style.backgroundColor='White';this.style.color='#003399'");
         e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
         //单击/双击 事件
         e.Row.Attributes.Add("OnDblClick", "DbClickEvent('" + e.Row.Cells[1].Text + "')");
         e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[1].Text + "')");
         e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
         //设置悬浮鼠标指针形状为"小手"
         e.Row.Attributes["style"] = "Cursor:hand";
        
}

5、将GridView的列的Visible设置为false时,该列无法更新到数据库