zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【贪心】【堆】Gym
    【并查集】Gym
    【拓扑排序】【bitset】Gym
    【递归】【线段树】【堆】AtCoder Regular Contest 080 E
    【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
    【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
    【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix
    【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
    【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists
    【推导】【贪心】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2045742.html
Copyright © 2011-2022 走看看