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

來源:互聯網
上載者:User

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

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.