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

本文介绍如何在ASP.NET应用中实现DataGrid的全选与反选功能,并通过JavaScript进行操作。同时,展示了如何为DataGrid的行与单元格添加鼠标悬停时的背景颜色变化效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、

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


              }


     }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值