我的代码
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//添加自定义属性,当鼠标移过来时设置该行的颜背景色为"#FFC0C0",并保持原背景色
e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#FFC0C0'");
//添加自定义属性,当鼠标移走时还原该行的背景色
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
}
请问代码里的this代表的是什么?为什么我页面上有4个datagrid,保错误this不存在,如果只有一个
datagrid就可以,请问为什么,this可以用什么代替

解决方案 »

  1.   


       this
     换成
       DataGrid的名字["+e.Item.ItemIndex+"] 
     试试
      

  2.   

    对e.Item进行命名
    e.Item.ID="DG1Row";
      

  3.   

    this就是e.item在客户端上的对象,也就是<tr>元素。
    你的代码把 currentcolor 声明为页面上的全局变量。这种设计根本没有考虑到页面上有多个此类控件时如何区分它们自己的局部变量的问题,并非技巧问题,属于设计原则方面基础就较差。
      

  4.   

    并不需要给e.item设置ID。即使设置了,Datagrid也不会输出<tr>的ID,不信可以看看客户端网页源代码。如果要设置ID,就要手工设置Attribute属性。不过不设置ID并不会影响this的访问。
      

  5.   

    <asp:DataGrid id="DataGrid1" runat="server" Width="544px" Height="224px">
    <SelectedItemStyle ForeColor="#0000C0" BorderColor="#FF80FF" BackColor="#C0C0FF"></SelectedItemStyle>
    </asp:DataGrid>
    它自己提供了"选择项模板",怎么不用?而用JavaScript?
      

  6.   

    给你一段 http://cmda618.vicp.net/w2cms 上的datagrid此处的代码参考:    Protected Overrides Sub OnItemDataBound(ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
            MyBase.OnItemDataBound(e)
            Select Case e.Item.ItemType
                Case ListItemType.AlternatingItem, ListItemType.Item, ListItemType.SelectedItem
                    With e.Item
                        With .Attributes()
                            .Add("onclick", Me.Page.GetPostBackEventReference(Me, Me.DataKeys.Item(e.Item.ItemIndex).ToString))
                            .Add("onMouseOver", "this.style.cursor='pointer'; this.style.backgroundColor='" + _
                                ColorTranslator.ToHtml(BackgroundColorWhenMouseOver) + "'")
                            .Add("onMouseOut", "this.style.backgroundColor='" + _
                                ColorTranslator.ToHtml(e.Item.BackColor) + "'")
                        End With
                    End With
            End Select
        End Sub
      

  7.   

    那个网站上的是旧程序,在onMouseOut处与上面的代码不同。上面代码是正确的。
      

  8.   

    其实意思是一样的,所以看懂上面的代码就行了。关键就是不需要过分依赖script。Protected Overrides Sub OnItemDataBound(ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
            MyBase.OnItemDataBound(e)
            Select Case e.Item.ItemType
                Case ListItemType.AlternatingItem, ListItemType.Item, ListItemType.SelectedItem
                    With e.Item
                        With .Attributes()
                            .Add("onclick", Me.Page.GetPostBackEventReference(Me, Me.DataKeys.Item(e.Item.ItemIndex).ToString))
                            .Add("onMouseOver", "this.style.cursor='pointer'; this.style.backgroundColor='" + _
                                ColorTranslator.ToHtml(BackgroundColorWhenMouseOver) + "'")
                        End With
                    End With
            End Select
            Select Case e.Item.ItemType
                Case ListItemType.AlternatingItem
                    With e.Item
                        With .Attributes()
                            .Add("onMouseOut", "this.style.backgroundColor='" + _
                                ColorTranslator.ToHtml(Me.AlternatingItemStyle.BackColor) + "'")
                        End With
                    End With
                Case ListItemType.Item
                    With e.Item
                        With .Attributes()
                            .Add("onMouseOut", "this.style.backgroundColor='" + _
                                ColorTranslator.ToHtml(Me.ItemStyle.BackColor) + "'")
                        End With
                    End With
                Case ListItemType.SelectedItem
                    With e.Item
                        With .Attributes()
                            .Add("onMouseOut", "this.style.backgroundColor='" + _
                                ColorTranslator.ToHtml(Me.SelectedItem.BackColor) + "'")
                        End With
                    End With
            End Select
        End Sub
      

  9.   

    不要用this,直接用datagrid的ID访问
      

  10.   

    給你一個前台控件datagrid选中变色的方案:
    <body onload="Page_Load();">
    function Page_Load()
    {
    var nodes = document.all.dgUserData.children;
    for(var i=0; i<nodes.length; i++)
    {
    if(nodes[i].tagName=="TBODY")
    {
    trNodes = nodes[i].children;
    UpdateTR(trNodes);
    }
    } }

    function UpdateTR(trs)
    {
    for(var i=0; i<trs.length; i++)
    {
    trs[i].onmouseover = MouseOver;
    trs[i].onmouseout = MouseOut;
    trs[i].onclick = Click;
    }
    }
                                function MouseOver()
    {
    if(this!=currentTR)
    {
    this.style.backgroundColor = "#FF0000";
    }
    }
    function MouseOut()
    {
    if(this!=currentTR)
    {
    this.style.backgroundColor="";
    }
    }
    function Click()
    {
    if(currentTR!=null) currentTR.style.backgroundColor="";
    currentTR = this;
    this.style.backgroundColor="#00ff00";
    }
      

  11.   

    private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item )
    {
    e.Item.Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#C8F7FF'");
    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor");
    for (int i = 0; i< DataGrid1.Columns.Count; i++ )
    {
    e.Item.Cells[i].Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#99ccff'");
    e.Item.Cells[i].Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor");
    }
    }
    }