DataGrid和GridView单击背景变色双击颜色还原
DataGrid中
首先我们假设.aspx文件中DataGrid的数据行的样式为
<AlternatingItemStyle BackColor="White" ForeColor="#284775" /> <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
则在DataGrid的ItemDataBound事件中添加如下代码即可
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { e.Item.Attributes["onClick"] = "javascript:c=this.style.backgroundColor;this.style.background='#FFA500';"; //current 粉蓝色 //--#6699ff 蓝色 #FFFF00 黄色 #FFFFE0 亮黄色 if (e.Item.ItemType == ListItemType.Item) { e.Item.Attributes["onDblClick"] = "javascript:this.style.background='#F7F6F3';"; } else { e.Item.Attributes["onDblClick"] = "javascript:this.style.background='#FFFFFF';"; } }
GridView中
首先我们假设.aspx文件中GridView的数据行的样式为
<AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333"/>
则在GridView的RowDataBound事件中添加如下代码即可
if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onClick"] = "javascript:c=this.style.backgroundColor;this.style.background='#FFA500';"; if (e.Row.RowState == DataControlRowState.Normal) { e.Row.Attributes["onDblClick"] = "javascript:this.style.background='#F7F6F3';"; } if (e.Row.RowState == DataControlRowState.Alternate) { e.Row.Attributes["onDblClick"] = "javascript:this.style.background='White';"; } }