单击以选择gridview中的行,从而在编辑模式下导致回发

时间:2012-10-05 20:06:38

标签: asp.net select gridview onclick edit

目前,当用户点击某一行的任何位置时,它会选择该行。不幸的是,当处于编辑模式(点击“编辑”之后)时,每次用户点击表单上的任何地方时,表单都会进行回发。如何禁用此功能并让用户安静地编辑该行?

这就是我所拥有的点击选择行:

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            List<int> notClickable = new List<int>();
            {
                notClickable.Add(0);
            }

            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (!notClickable.Contains(i))
                {
                    e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                    e.Row.Cells[i].Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                }
            }

            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.ToolTip = "Click to select row";
        }
    }

解决问题的修订代码:

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (GridView1.EditIndex != -1)
            {
                e.Row.Attributes.Remove("onmouseover");
            }
            else
            {
                List<int> notClickable = new List<int>();
                {
                    notClickable.Add(0);
                }

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (!notClickable.Contains(i))
                    {
                        e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                        e.Row.Cells[i].Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                    }
                }

                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#ceedfc'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle");
                e.Row.Attributes.Add("style", "cursor:pointer;");
                e.Row.ToolTip = "Click to select row";
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

在您的选择功能上,让它删除onclick属性。完成编辑后,将其重新添加。

相关问题