【问题标题】:Gridview SelectedIndex "lag" with postbackGridview SelectedIndex“滞后”与回发
【发布时间】: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 行 - 它始终位于所选行的“后面”。

【问题讨论】:

    标签: asp.net gridview postback


    【解决方案1】:

    我没有触发 Gridview.OnSelectedIndexChanged 事件。通过添加一个在触发时重绘页面的方法来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多