读书人

仿163邮箱gridview解决方法

发布时间: 2012-01-03 22:16:06 作者: rapoo

仿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 == ListItemType.EditItem || e.Item.ItemType == ListItemType.SelectedItem)



[解决办法]
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
e.Item.Rows[e.Item.ItemIndex].Attributes.Add( "onmouseout ", "edit( ' "+e.Item.Cells[1].Text+ " ') ");
e.Item.Rows[e.Item.ItemIndex].Attributes.Add( "onmouseover ", "edit( ' "+e.Item.Cells[1].Text+ " ') ");
}


function table(i)
{
if(i!=-1)
{
var DataGrid=document.getElementById( " <%=DataGrid1.ClientID%> ");
var chk=DataGrid.rows[i+1].cells[0].getElementsByTagName( "INPUT ")[0];
if(chk.checked)
{
DataGrid.rows[i+1].style.background= "#EEF7FE ";
DataGrid.rows[i+1].style.color= "blue ";
chk.checked=true;
}
else
{
DataGrid.rows[i+1].style.background= "white ";
DataGrid.rows[i+1].style.color= "black ";
chk.checked=false;
}
}
}

读书人网 >asp.net

热点推荐