【问题标题】:Gridview row clickable except for first column?除了第一列之外,Gridview 行可以点击吗?
【发布时间】:2011-04-27 01:50:41
【问题描述】:

我正在使用以下代码使我的 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);
        }
    }

效果很好,除了现在我想为网格添加编辑功能。这可行,但是当我同时打开行可单击和编辑功能时,单击“编辑”链接按钮通常会触发行单击事件,反之亦然。

那么,除了指定的列之外,我怎样才能保持行可点击?

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

基于贾斯汀的解决方案:

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    诀窍是在需要可点击的特定列上注册点击。下面的代码假设您知道应该可以点击的索引(在本例中为 0)。

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

    【讨论】:

    • 我应该把这段代码放在哪里?我可以遍历行中的单元格,使单元格 2-n 可点击吗?
    • 把它放在你当前编码的 onclick 的地方。将您的 e.Row.Attributes 行替换为 e.Row.Cells[0].Attributes.
    • @MAW74656 - 是的,您可以遍历单元格。 e.Row.Cells.Length 我相信会给你单元格的数量,它可能是 e.Row.Cells.Count。
    • 完美,我会尽快尝试,然后我会接受答案。
    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 2012-03-03
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多