在后台DataGrid的DataBound中为前台添加了下列事件,作用是当鼠标点击某行时执行fortry函数,同时让该行变色。但是在点击另一行时原来变色的那行应该恢复原色才行,而下面的代码则是点击了哪几行哪几行就变色。请问如何做到始终只有被点击的行变色?
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","fortry(this);");
e.Item.Attributes.Add("onmousedown", "this.style.backgroundColor='#ccffff';this.style.color='';"); 
e.Item.ID = "ItemID";
}

解决方案 »

  1.   

    不要用click事件,用mousemove事件,参照:
    <tr valign='middle' onMouseOver="this.style.backgroundColor='#EAEAEA';" onMouseOut="this.style.backgroundColor=''">
      

  2.   

    楼上的,click事件是一定要用的,因为还有其它的用场。现在要实现的效果就是点击该行,该行变色,点击另一行另一行变色,而原来被点击的变色的行又恢复原状。
      

  3.   

    Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
           e.Item.Attributes.Add("onclick","if(this.style.backgroundColor != '#6699ff') {this.style.backgroundColor='#6699ff';} else {this.style.backgroundColor='#ffffff';}")       End Sub
      

  4.   

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
            If (e.Item.ItemIndex >= 0) Then
                e.Item.Attributes("onmouseover") = "this.name=this.style.backgroundColor;this.style.backgroundColor='Honeydew';this.style.color='blue'"
                e.Item.Attributes("onmouseout") = "this.style.backgroundColor=this.name;this.style.color='black'"
            End If
                End Sub