Gridview一般朋友们都比较常用,因为它可以方便快捷的实现我们所需的很多功能,代码也比较简洁。平时的项目中这个控件我也比较常用,其中有个功能用到的频率也比较多。所以记录下备忘。
功能描述:
在gridview中,鼠标在这个控件的数据列表中移动时,该列的背景随鼠标的移动而改变背景颜色。
功能实现:
在gridview中新增一个事件RowDataBound,代码如下:
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
protected
void
gvwNews_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add( " onmouseover " , " color = this.style.backgroundColor;this.style.backgroundColor='#EAFCD5' " );
e.Row.Attributes.Add( " onmouseout " , " this.style.backgroundColor=color " );
e.Row.Attributes.Add( " onclick " , " ClickRow() " );
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add( " onmouseover " , " color = this.style.backgroundColor;this.style.backgroundColor='#EAFCD5' " );
e.Row.Attributes.Add( " onmouseout " , " this.style.backgroundColor=color " );
e.Row.Attributes.Add( " onclick " , " ClickRow() " );
}
}
其中的onclick事件是控制选择行首的复选框的,点击行便实现"点击"复选框一样的效果。代码如下:
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
function
ClickRow()
{
var obj = event.srcElement.parentElement.firstChild.firstChild.tagName;
alert(obj);
if (obj != null && obj.tagName + "" != " undefined " )
{
obj.checked = obj.checked ? false : true ;
}
}
{
var obj = event.srcElement.parentElement.firstChild.firstChild.tagName;
alert(obj);
if (obj != null && obj.tagName + "" != " undefined " )
{
obj.checked = obj.checked ? false : true ;
}
}
简单的几行代码所需功能就可以实现了。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-665043/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-665043/