有时候我们需要在删除DataGrid中Item相对应的数据时,需要弹出一个确认对话框来提示使用者,其实这个功能非常简单,下面的代码可以在DataGrid的Item 中产生颜色交替的效果。    

 private void dg_ItemDataBound(object sender, DataGridItemEventArgs e)

  {

   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

   {

 //删除确认            

    LinkButton delBttn = (LinkButton) e.Item.Cells[1].Controls[0];

    delBttn.Attributes.Add("onclick","javascript:return confirm('确定删除" + e.Item.Cells[4].Text + "?');"); 

 //颜色交替   

    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='seashell'");

    if(e.Item.ItemType == ListItemType.Item)

    {

     e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");

    }

    if(e.Item.ItemType ==ListItemType.AlternatingItem)

    {

     e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='seashell'");

    }

   }

  }