GridView 单击选择行,双击打开详细页面,鼠标移到某行上变色

protected void gvwDepartment_RowDataBound(object sender, GridViewRowEventArgs e)   
{//判断是否是数据行   
    if (e.Row.RowState == DataControlRowState.Edit)   
    { // 编辑状态    
        e.Row.Attributes.Remove("onclick");   
        e.Row.Attributes.Remove("ondblclick");   
        e.Row.Attributes.Remove("style");   
        e.Row.Attributes["title"] = "编辑行";   
    }   
     if (e.Row.RowType == DataControlRowType.DataRow)   
    {   
        //鼠标移动到某行上,该行变色   
        e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddee'");   
        //鼠标移开后,恢复   
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");   
        //鼠标显示为小手状态   
        e.Row.Attributes["style"] = "Cursor:hand";   
    }   
}   
protected override void Render(HtmlTextWriter writer)   
{   
    // GridView    
    foreach (GridViewRow row in gvwDepartment.Rows)   
    {   
        if (row.RowState == DataControlRowState.Edit)   
        { // 编辑状态    
            row.Attributes.Remove("onclick");   
            row.Attributes.Remove("ondblclick");   
            row.Attributes.Remove("style");   
            row.Attributes["title"] = "编辑行";   
            continue;   
        }   
        if (row.RowType == DataControlRowType.DataRow)   
        {   
            // 单击事件,为了响应双击事件,需要延迟单击响应,根据需要可能需要增加延迟    
            // 获取ASP.NET内置回发脚本函数,返回 __doPostBack(<<EventTarget>>, <<EventArgument>>)    
            // 可直接硬编码写入脚本,不推荐                    
            row.Attributes["onclick"] = String.Format("javascript:setTimeout(\"if(dbl_click){{dbl_click=false;}}else{{{0}}};\", 1000*0.3);", ClientScript.GetPostBackEventReference(gvwDepartment, "Select$" + row.RowIndex.ToString(), true));   
            // 双击,设置 dbl_click=true,以取消单击响应    
            row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.location.href='UpdateDepartment.aspx?DepartmentID={0}';", gvwDepartment.DataKeys[row.RowIndex].Value.ToString());   
            //    
            row.Attributes["style"] = "cursor:pointer";   
            row.Attributes["title"] = "单击选择行,双击打开详细页面";   
        }   
    }   
  
    base.Render(writer);   
}  
    protected void gvwDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
    {//判断是否是数据行
        if (e.Row.RowState == DataControlRowState.Edit)
        { // 编辑状态 
            e.Row.Attributes.Remove("onclick");
            e.Row.Attributes.Remove("ondblclick");
            e.Row.Attributes.Remove("style");
            e.Row.Attributes["title"] = "编辑行";
        }
         if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标移动到某行上,该行变色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddee'");
            //鼠标移开后,恢复
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            //鼠标显示为小手状态
            e.Row.Attributes["style"] = "Cursor:hand";
        }
    }
    protected override void Render(HtmlTextWriter writer)
    {
        // GridView 
        foreach (GridViewRow row in gvwDepartment.Rows)
        {
            if (row.RowState == DataControlRowState.Edit)
            { // 编辑状态 
                row.Attributes.Remove("onclick");
                row.Attributes.Remove("ondblclick");
                row.Attributes.Remove("style");
                row.Attributes["title"] = "编辑行";
                continue;
            }
            if (row.RowType == DataControlRowType.DataRow)
            {
                // 单击事件,为了响应双击事件,需要延迟单击响应,根据需要可能需要增加延迟 
                // 获取ASP.NET内置回发脚本函数,返回 __doPostBack(<<EventTarget>>, <<EventArgument>>) 
                // 可直接硬编码写入脚本,不推荐                 
                row.Attributes["onclick"] = String.Format("javascript:setTimeout(\"if(dbl_click){{dbl_click=false;}}else{{{0}}};\", 1000*0.3);", ClientScript.GetPostBackEventReference(gvwDepartment, "Select$" + row.RowIndex.ToString(), true));
                // 双击,设置 dbl_click=true,以取消单击响应 
                row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.location.href='UpdateDepartment.aspx?DepartmentID={0}';", gvwDepartment.DataKeys[row.RowIndex].Value.ToString());
                // 
                row.Attributes["style"] = "cursor:pointer";
                row.Attributes["title"] = "单击选择行,双击打开详细页面";
            }
        }

        base.Render(writer);
    }

view plaincopy to clipboardprint?
protected void gvwDepartment_SelectedIndexChanged(object sender, EventArgs e)   
{//设置选中行的颜色   
    gvwDepartment.SelectedRowStyle.BackColor = Color.FromArgb(222, 110, 222, 210);   
}   
protected void gvwDepartment_RowCreated(object sender, GridViewRowEventArgs e)   
{//将GRIDVIEW的第一列,即选择列隐藏   
    e.Row.Cells[0].Attributes.Add("style", "display:none;");   
}  



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bnmjstu/archive/2009/07/17/4356430.aspx

功能: 单击选中行,双击打开详细页面
说明:
单击事件(onclick)使用了 setTimeout 延迟,根据实际需要修改延迟时间
当双击时,通过全局变量 dbl_click 来取消单击事件的响应
常见处理行方式会选择在 RowDataBound/ItemDataBound 中处理,这里我选择 Page.Render 中处理,至少基于以下考虑
1、RowDataBound 仅仅在调用 DataBind 之后才会触发,回发通过 ViewState 创建空件不触发 假如需要更多的处理,你需要分开部分逻辑到 RowCreated 等事件中
2、并且我们希望使用 ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法 进行安全脚本的注册,而后者需要在页的 Render 阶段中才能处理

注意:需要在前台加入javascript

<script type="text/javascript">
    // 辅助全局变量,指示是否双击
    var dbl_click = false;
</script>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bnmjstu/archive/2009/07/17/4356430.aspx

posted @ 2011-05-13 17:45  小锋神  阅读(3275)  评论(0编辑  收藏  举报