DataGrid中加多选按钮 和 鼠标移动显示不同的颜色

来源:互联网 发布:2010人口普查全国数据 编辑:程序博客网 时间:2024/05/29 15:11

一、

aspx文件中加
<script language="javascript">
  <!--
  //CheckBox全选And反全选
  function select_deselectAll (chkVal, idVal)
  {
   var frm = document.forms[0];
   for (i=0; i<frm.length; i++)
   {
    if (idVal.indexOf ('CheckAll') != -1)
    {
     if(chkVal == true)
     {
      frm.elements[i].checked = true;
     }
     else
     {
      frm.elements[i].checked = false;
     }
    }
    else if (idVal.indexOf('DeleteThis') != -1)
    {
     if(frm.elements[i].checked == false)
     {
      frm.elements[1].checked = false;
     }
    }
   }
         }
   //-->
</script>

全选<input id="CheckAll" onclick="return select_deselectAll (this.checked, this.id)" tabIndex="0" type="checkbox" title="点击全选或反全选当前页所有信息">

DataGrid中加模板列
<asp:TemplateColumn HeaderText="选定">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemTemplate>
<asp:CheckBox id="chkSelectBox" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

CS文件中操作选定项
foreach (DataGridItem item in DataGrid.Items)
{
 if(((CheckBox)item.FindControl("chkSelectBox")).Checked==true)
 {
  Response.Write (item.Cells[0].Text);
 }
}

 

二、

 

核心代码:


private void grdCustomer_ItemDataBound(object sender,


         System.Web.UI.WebControls.DataGridItemEventArgs e)


{


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


              e.Item.ItemType == ListItemType.Item )


     {


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


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


          for (int i = 0; i< grdCustomer.Columns.Count; i++ )


              {


                   e.Item.Cells[i].Attributes.Add("onmouseover",


          "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#99ccff'");


                   e.Item.Cells[i].Attributes.Add(


                       "onmouseout", "this.style.backgroundColor=this.oldcolor");


              }


     }


}