asp.net - 单击以选择 GridView 中的行,导致编辑模式下回发

标签 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";
            }
        }
    }

最佳答案

在您的选择函数上,让它删除 onclick 属性。然后,当您完成编辑后,将其添加回来。

关于asp.net - 单击以选择 GridView 中的行,导致编辑模式下回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753318/

相关文章:

MySQL - 如何在一个(UNION)查询中选择最小值和最大值

在字符串中使用点 (.) 搜索值时,MySQL 查询返回 0 行

c# - 如何使用 JavaScript 仅刷新父窗口中的 GridView?

gridview - yii2 gridview过滤器在相关列上

asp.net - 通过 CSS 截断 GridView 列标题文本

jquery - 尝试本地化 jQuery 移动日期选择器输入

c# - 请等待按钮上的加载栏单击未显示

c# - 如何通过继承访问对象

MySQL 对多个 OR 使用索引,但对 IN 没有索引而且速度慢得多

c# - 如何在不重复的文本框中制作图像