【发布时间】:2011-02-21 15:11:03
【问题描述】:
我怀疑这是因为我对 asp.net 中的页面生命周期不够了解,但我有一个奇怪的问题。
我有一个gridview,用户从中选择一行。所选行应为“浅绿色”,其余为白色。
我在 RowDataBound 上使用了一个事件来制作“荧光笔”来突出显示用户悬停的行。当用户鼠标移出时,它应该恢复到以前的颜色。 (未选中的行为白色,选中的行为浅绿色。)
protected void gvCounts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (gvCounts.SelectedIndex == -1) { gvCounts.SelectedIndex = 0; }
string oldColor = "white";
if (e.Row.RowIndex == gvCounts.SelectedIndex)
{
e.Row.BackColor = System.Drawing.Color.Aqua;
oldColor = "aqua";
}
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='yellow';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='" + oldColor + "';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvCounts, "Select$" + e.Row.RowIndex);
}
}
由于某种原因,选定的行总是比用户点击的行慢一。当页面加载时,选定的行设置为 0。如果我然后单击第 2 行,页面刷新并且第 0 行仍然是浅绿色的。
如果我然后单击第 4 行,则第 2 行将成为选定行。如果我单击不同的行,将选择第 4 行 - 它始终位于所选行的“后面”。
【问题讨论】: