国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

在Web DataGrid中當鼠標移到某行與離開時行的顏色發生改變(結合javascript)

2019-11-18 19:21:29
字體:
來源:轉載
供稿:網友

在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");
 }

  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临潭县| 宜兴市| 北海市| 秦皇岛市| 安庆市| 淮南市| 涿州市| 舞阳县| 绥芬河市| 阆中市| 延吉市| 綦江县| 西峡县| 旌德县| 马鞍山市| 尚义县| 疏勒县| 涡阳县| 江华| 临沧市| 老河口市| 烟台市| 绍兴市| 潜山县| 石河子市| 阿拉善右旗| 新津县| 勐海县| 弥勒县| 响水县| 邯郸县| 栾川县| 仙桃市| 洪湖市| 陵川县| 南丰县| 油尖旺区| 比如县| 连城县| 双桥区| 车致|