【问题标题】:Making rows containing DropDownList in a GridView clickable, breaks DropDownList使 GridView 中包含 DropDownList 的行可点击,会破坏 DropDownList
【发布时间】:2012-03-10 06:28:28
【问题描述】:

我使用以下代码和 RaisePostBackEvent 函数使 GridView 中的行可点击。

protected void gvResults_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(this, e.Row.RowIndex.ToString()));
    }
}

GridView 还具有用于其中一个单元格的 DropDownList 控件。问题是单击 DropDownList 更改选择也会触发该行的 onclick。这会导致 DropDownList 的选项列表可见但无法使用,因为该行的 onclick 也已被处理。

我尝试在 DropDownList 的 onmouseover 中设置一个标志,并在 RaisePostBackEvent 函数中检查它,但这只是部分成功。这阻止了 RaisePostBackEvent 中的功能,但行 onclick 仍然发生,这意味着 DropDownList 仍处于不可用状态。

那么,当点击该行的其他部分时,如何在创建 DropDownList 功能的同时仍然允许 onclick 起作用?

注意:我目前受限于使用 .NET 2.0。

【问题讨论】:

标签: asp.net gridview


【解决方案1】:

您要做的是停止传播 Click 事件。最简单的方法是将元素包装在停止传播的 div/span 元素中:

 <div onclick="if (event.stopPropagation){ event.stopPropagation(); } else if(window.event){ window.event.cancelBubble=true; } ">
 DropDownList element here...
 </div>

IE 使用 event.cancelBubble 属性,而其他浏览器使用 stopPropagation。

我不确定您是如何在 Gridview 中生成 DropDownList 元素的 - 您可以在 ItemTemplate 中添加它,或者在 RowCreated 事件中创建元素时找到一种方法在元素周围添加它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多