有一个Datagrid,现在要求鼠标放在一行上,该行就会变色,在这一行任意空白处点击,该行也会变色?用Javascript吧,没有找到合适的例子。

解决方案 »

  1.   

    很简单的啊,两个同理的吧 onCilk事件 和 onMouseOver事件
      

  2.   

    // ItemDataBound事件
    private void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    //如果是数据项并且是交替项
    if(e.Item.ItemType == ListItemType.Item  || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    //添加自定义属性,当鼠标移过来时设置该行的背景色为"6699ff",并保存原背景色
    e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
    //添加自定义属性,当鼠标移走时还原该行的背景色
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor"); } 
    }
      

  3.   

    模版列
    <script> function  a(obj)
    {
    obj.bgColor="red";
    }
    <script><table onclick="a(this)" onmousermove="...">
    帮定
    <table>
      

  4.   


    <script> function  a(obj)
    {
    obj.bgColor="red";
    }
    <script><table onclick="a(this)" onmousermove="..."><table>
      

  5.   

    http://www.cnblogs.com/smilnet/articles/25049.html
      

  6.   

    借花献佛
    在datgrid的MyDataGrid_ItemDataBound事件中:
    private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemIndex>=0)
    {
    e.Item.Attributes.Add("onmouseover","ItemOver(this)");//////在每行上增加脚本处理 onmouseover
    e.Item.Attributes.Add("onmouseout","ItemOut(this)");//////在每行上增加脚本处理 onmouseout
    }
    }
    在页面的代码中加js脚本:
    <script language="javascript">
    if (!objbeforeItem)
    {
    var objbeforeItem=null;
    var objbeforeItembackgroundColor=null;
    }
    function ItemOver(obj)
    {
    objbeforeItembackgroundColor=obj.style.backgroundColor;
    obj.style.backgroundColor="#92d4fe";
    objbeforeItem=obj;
    }function ItemOut(obj)
    {
    if(objbeforeItem)
    {
    objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
    }
    }
    </script>
      

  7.   

    在head中添加javascript 代码如下:<script lang=javascript>
     function sel(i) // 鼠标移上去后执行
     {
      eval(i+".style.background='#CCCC66'"); // 更改行的颜色
      eval(i+".style.cursor='hand'"); // 鼠标移上去后变为手形
     }
     function unsel(i) // 鼠标离开后执行
     {
      eval(i+".style.background=''");
     }
     function clicktr(i)
     {
      eval(i+".style.background=''");
      window.open("Edit.aspx?param="+i,"修改","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,
    menubar=no,location=no,left=50,top=50");
     }
    </script>在DataGrid的 ItemDataBound (当数据绑定时发生)事件中:private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
      {   
       if(e.Item.ItemType != ListItemType.Header)
       {
        string ID = e.Item.Cells[0].Text;     // 这里的第一列为数据绑定中的ID值(为修改页中传递参数方便,若多参数,也可按需要增加!)
        e.Item.Attributes.Add("id",ID);
        e.Item.Attributes.Add("onmouseover","sel(" + ID+ ")");    
        e.Item.Attributes.Add("onmouseout", "unsel(" + ID+ ")");
        e.Item.Attributes.Add("onclick", "clicktr(" + ID+")");
       }
      }
    //****************************     结束    **********************************************//不过以上做法存在不便之处,如果在DataGrid中加个模板列,用于给用户提供选择操作(比如删除选中),
    此时用上述方法就会造成每次在选择CheckBox的时候也弹出新窗口(激发了onclick事件)比较差的解决办法:将原先的基于行的 Attributes 改为基于列.除掉模板列外,所有列都添加属性.比如模板列在第6列,可以这样修改 cs 文件private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
      {   
       if(e.Item.ItemType != ListItemType.Header)
       {
        string bm = e.Item.Cells[0].Text;
        for(int i=0;i<5;i++)
        {
         e.Item.Cells[i].Attributes.Add("id","a"+i.ToString()+bm);
         e.Item.Cells[i].Attributes.Add("onmouseover","sel(" +i.ToString()+","+ bm + ")");    
         e.Item.Cells[i].Attributes.Add("onmouseout", "unsel(" +i.ToString()+","+ bm + ")");
         e.Item.Cells[i].Attributes.Add("onclick", "clicktr(" + bm +")");
        }
     }
    }
    在 javascript 代码中: function sel(i,ID)
     { 
      for(var j=0;j<5;j++) 
      { eval("a"+j.toString()+ID+".style.background='#CCCC66'"); eval("a"+j.toString()+ID+".style.cursor='hand'"); 
      } 
    }
    function unsel(i,ID)
    {
       for(var j=0;j<5;j++) 
      { eval("a"+j.toString()+ID+".style.background=''"); 
     } 

    function clicktr(i) 

      for(var j=0;j<5;j++) 
      { 
        eval("a"+j.toString()+i+".style.background=''"); 
        window.open("Edit.aspx?param="+i,"修改","height=490,width=710,resizable=no,scrollbars=no,status=no,toolbar=no,
    menubar=no,location=no,left=50,top=50");
     }  }