JS控制GridView行选择

ASP.NET里的GridView控件使用非常广泛,虽然其功能强大,但总有一些不尽如人意的地方。
比如在选择行的时候,它就没有UltraWebGrid做的友好;UltraWebGrid允许用户设置是否显示选择框,设置允许,则会在最左边多出一列,表示选择的行。而GridView则没有这个功能。但没有,不代表不能实现,下面我就通过JS来实现行选择的标识。

后台代码:

 

protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
{
    
int id = Convert.ToInt32(InstallandMaintanceGrid.DataKeys[e.Row.RowIndex].Value.ToString());
    e.Row.Attributes.Add(
"ondblclick""javascript:dbClick(" + id + ")");
    e.Row.Attributes.Add(
"id", _i.ToString());
    e.Row.Attributes.Add(
"onKeyDown""SelectRow();");
    e.Row.Attributes.Add(
"onClick""MarkRow(" + _i.ToString()+","+id + ");");
    _i
++;
}


 

前台代码:

  < script type = " text/javascript " >
            
function  dbClick(keys) {
                
//双击,获取该行ID
                document.getElementById("ctl00_hd_selectedid").value = keys;
            }

            
var  currentRowId  =   0 ;    
            
function  SelectRow() // 选择行
             {
                
if (event.keyCode == 40)
                    MarkRow(currentRowId
+1);
                
else if (event.keyCode == 38)
                    MarkRow(currentRowId
-1);
            }
        

            
function  MarkRow(rowId,keys) // 选中行变色
             {
                
if (document.getElementById(rowId) == null)
                    
return;            
                
if (document.getElementById(currentRowId) != null )
                
{
                    document.getElementById(currentRowId).style.backgroundColor 
='#ffffff';
                }


                currentRowId 
= rowId;
                document.getElementById(rowId).style.backgroundColor 
= '#ff0000';
                document.getElementById(
"ctl00_hd_selectedid").value = keys;
            }

        
</ script >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值