日期:2014-05-18  浏览次数:20474 次

仿163邮箱gridview
一带checkbox的GRIDVIEW,现在已完成的功能:选择第一列的checkbox,选中行,并改变行颜色.前台代码就不贴了,后台的:

  protected   void   GridView1_RowDataBound(object   sender,   GridViewRowEventArgs   e)
        {

                if   (e.Row.RowType   ==   DataControlRowType.DataRow)
                {
                        if   ((CheckBox)e.Row.FindControl( "CheckBox1 ")   !=   null)
                        {

                                CheckBox   cbox   =   (CheckBox)e.Row.Cells[0].FindControl( "CheckBox1 ");

                                if   (!cbox.Checked)   //判断是行是否被选中
                                {
                                                                                //当鼠标放上去的时候   先保存当前行的背景颜色   并给附一颜色
                                        e.Row.Attributes.Add( "onmouseover ",   "currentcolor=this.style.backgroundColor;this.style.backgroundColor= 'yellow '; ");

                                        //当鼠标离开的时候   将背景颜色还原的以前的颜色
e.Row.Attributes.Add( "onmouseout ",   "this.style.backgroundColor=currentcolor,this.style.fontWeight= ' '; ");
                                         

                                }
                        }
                }

        }
我知道,在选中checkbox之前鼠标事件就执行了,

e.Row.Attributes.Add( "onmouseover ",   "currentcolor=this.style.backgroundColor;this.style.backgroundColor= 'yellow '; ");
当我选中后,鼠标移出之前.改变选定行背景.
鼠标移出之行后,执行:
e.Row.Attributes.Add( "onmouseout ",   "this.style.backgroundColor=currentcolor,this.style.fontWeight= ' '; ");
颜色又为最初的背景色了.

以上代码有何问题,为什么GridView1是所有行都变色,虽然checkbox的状态没有变,但是选中行的颜色变了,在这个事件中:
  if   (!cbox.Checked)   //判断是行是否被选中
这句根本不判断.这是为什么,我该怎么办.
我要鼠标移动时,不要改变选定行的颜色,就像自\带的 "选择 "项一样


------解决方案--------------------
建议你调试下
或者将 CheckBox cbox = (CheckBox)e.Row.Cells[0].FindControl( "CheckBox1 ");

if (!cbox.Checked) //判断是行是否被选中
{ ...}

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