我的代码
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可以用什么代替
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可以用什么代替
this
换成
DataGrid的名字["+e.Item.ItemIndex+"]
试试
e.Item.ID="DG1Row";
你的代码把 currentcolor 声明为页面上的全局变量。这种设计根本没有考虑到页面上有多个此类控件时如何区分它们自己的局部变量的问题,并非技巧问题,属于设计原则方面基础就较差。
<SelectedItemStyle ForeColor="#0000C0" BorderColor="#FF80FF" BackColor="#C0C0FF"></SelectedItemStyle>
</asp:DataGrid>
它自己提供了"选择项模板",怎么不用?而用JavaScript?
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
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
<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";
}
{
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");
}
}
}