GridView行可点击,除了第一列?

6

我正在使用以下代码使我的 gridview 的整行可点击:

 protected void gridMSDS_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.backgroundColor='#EEFF00'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='White'";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);
        }
    }

这个功能很好用,但是现在我想给网格添加编辑能力。虽然这样做可以实现,但当我同时打开行可点击和编辑功能时,点击“编辑”链接按钮经常会触发行点击事件,反之亦然。

那么,如何保持行可点击,但指定列除外呢?

更新: 这是我正在使用的代码。

基于Justin的解决方案:

 List<int> notClickable = new List<int>();
 {
       notClickable.Add(0);
       notClickable.Add(2);
 }

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

4

技巧在于注册需要可点击的特定列上的单击事件。以下代码假定您知道应该可点击的索引(在本例中为0)。

e.Row.Cells[0].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);

我应该把这段代码放在哪里?我能循环遍历行中的单元格,使2-n的单元格可点击吗? - MAW74656
将当前编码的onclick内容放在相应位置,用e.Row.Cells[0].Attributes替换你的e.Row.Attributes。 - Justin C
@MAW74656 - 是的,你可以循环遍历单元格。我相信 e.Row.Cells.Length 可以给你单元格的数量,也可能是 e.Row.Cells.Count。 - Justin C
太好了,我会尽快试一下,然后接受这个答案。 - MAW74656

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接