DataGrid删除确认及Item颜色交替

后台代码:
VB代码:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        '删除确认
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
                Dim myDeleteButton As LinkButton
                myDeleteButton = e.Item.Cells(3).Controls(0)
                myDeleteButton.Text = "删除此行"
                myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除第 " + CType(e.Item.ItemIndex + 1, String) + " 行吗?');")
        End Select
        '行背景颜色交替显示
        Dim wcc As New WebColorConverter
        e.Item.BackColor = CType(wcc.ConvertFromString("#99ccff"), Color)
        If e.Item.ItemType = ListItemType.Item Then
            e.Item.BackColor = Color.White
            'e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#ffffff'")
        ElseIf e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.BackColor = CType(wcc.ConvertFromString("#99ccff"), Color)
            'e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#99ccff'")
        End If
    End Sub

C#代码:
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'");
    }
   }
  }

 

你可能感兴趣的:(datagrid)