开发者

Gridview SelectedIndex "lag" with postback

I suspect this is down to me not knowing enough about Page Lifecycle in asp.net but I have an 开发者_运维知识库odd problem.

I have a gridview, which the user selects a row from. The selected row should be colored "aqua", the rest are white.

I use an event on RowDataBound to make a "highlighter" to highlight the row the user hovers over. When the user mouses out, it should revert to the color it was previously. (White for a non-selected row, or aqua for the selected row.)

  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);
        }
    }

For some reason the selected row is always one behind what the user clicks. When the page forst loads, the selected row is set to 0. If I then click on row 2, the page refreshes and row 0 is still aqua.

If I then click row 4, row 2 becomes the selected row. If I click a different row, row 4 will be selected - it is always one "behind" the row that was selected.


I wasn't firing a Gridview.OnSelectedIndexChanged event. Fixed this by adding a method to redraw the page when this fired.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜