一定要用e.Item.Attributes.Add("onclick", "this.style.backgroundColor='Gray';")
实现,在中间还加点语句。

解决方案 »

  1.   

    dim i as integer
    dim count as integer =datagrid.items.count
    for i=0 to count-1
       datagrid.items(i).Attributes.Add("onMouseOver","Currentcolor=this.style.backgroundcolor;" "this.style.backgroundColor='Gray';")
    datagrid.items(i).Attributes.Add("onMouseOut","this.style.backgroundColor=Currentcolor;")
    next
      

  2.   

    如果觉得这问题简单,就用我说的做出来啊。e.Item.Attributes.Add("onclick", "this.style.backgroundColor='Gray';这里还加点代码啊")
      

  3.   

    做个例子:
    <script language=javascript>
    function doclick(tab)
    {
    if(event.srcElement.tagName!="TD") return;
    if(tab.oldTr!=null)
    tab.oldTr.style.backgroundColor="#ffffff";
    tab.oldTr=event.srcElement.parentElement;
    tab.oldTr.style.backgroundColor="#aaaaaa";
    }
    </script>
    <table border=0 width=200 onclick="doclick(this)">
    <tr><td style="border:1px solid #336600">第一行</td></tr>
    <tr><td style="border:1px solid #336600">第二行</td></tr>
    <tr><td style="border:1px solid #336600">第三行</td></tr>
    <tr><td style="border:1px solid #336600">第四行</td></tr>
    <tr><td style="border:1px solid #336600">第五行</td></tr>
    </table>
      

  4.   

    再加一个onmouseout,onmouseovere.Item.Cells(i).Attributes.Add("onmouseover", "this.style.backgroundColor='Gray'")
    e.Item.Cells(i).Attributes.Add("onmouseout", "this.style.backgroundColor='white'")
    e.Item.Attributes.Add("onclick", "this.style.backgroundColor='Gray';")'white'这个就可以是你点击前的颜色了
      

  5.   

    好象不合你的要求你的点击也不是datagrid真正的选择了这行,是在点击其他行时改变前一行的颜色,up下
      

  6.   

    后台(xx.aspx.vb)写上:
    Private Sub dgGrid_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgGrid.ItemCreated
                Dim selectID As Integer         '----selectID   所选中项的ID号
                Dim selectRows As Integer       '----selectRows   所选中项的行号
                selectRows = e.Item.ItemIndex
                If selectRows >= 0 Then
                    selectID = CInt(dgGrid.DataKeys(selectRows))
                End If
                e.Item.Attributes("style") = "cursor:hand"
                e.Item.Attributes("OnClick") = "dgClick(" & selectID & "," & selectRows & ")"
            End Sub页中(xx.aspx)写上 javascript:
    // 单击使 DataGrid 变色
    var OldIndex=1;
    function dgClick(selectID,selectRows)
    {
    //alert(event.srcElement.innerText)
    if (selectRows == -1)
    return false; // 选择表头或表尾无效
    var curIndex;
    curIndex=selectRows+1;
    var objTr;
    objTr="document.all['dgGrid'].rows(curIndex)"
    if (eval(objTr).bgColor=='#ffff00') 
    {
    eval(objTr).bgColor='';
    }
    else 
    {
    eval(objTr).bgColor='#ffff00'; }

    if (OldIndex!=curIndex)
    {
    document.all["dgGrid"].rows(OldIndex).bgColor=''
    }
    OldIndex=curIndex; // OldIndex记录当前点击的行号
    }对以上代码若有疑问再问我啦:)