如何阻止rowcommand事件在gridview中执行?

时间:2012-02-10 08:12:36

标签: asp.net

我在asp:TemplateField内有一个带有复选框的gridview。我希望选择多个复选框,然后在按钮上单击我想在另一个由Repeater组成的页面中添加这些选定的行。另外,我想让用户能够在任何行中选择一行点击。

我的问题是当我点击任何一个复选框时,rowcommand被触发,一条记录被添加到转发器。 我希望在选中/取消选中复选框时阻止此rowcommand事件被触发。

请在这个问题上指导我。 感谢。

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

1 个答案:

答案 0 :(得分:0)

我假设您已在整行上添加了javascript,以便在用户单击该行时选择该行。复选框是行的一部分,立即回发的原因是什么。因此,您需要将Javascript添加到所有单元格而不使用checkbox-cell。

您可以使用RowDataBound

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
       var chechBoxColumnIndex=1;
       var highlightColor = "yellow"; 
       var cells=(e.Row.Cells
                   .Cast<TableCell>()
                   .Where((r,index) => index != chechBoxColumnIndex));

        foreach(var c in cells.Cast<TableCell>()){
            //note: you need to set EnableEventValidation to false in this page
            c.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, "Select$" + e.Row.RowIndex);
            c.ToolTip = "Click to select row";
            c.Attributes["onmouseover"] = "this.style.cursor='pointer';bgColor=this.parentNode.style.backgroundColor;this.parentNode.style.backgroundColor= '" + highlightColor + "';";
            c.Attributes["onmouseout"] = "this.parentNode.style.backgroundColor=bgColor;";
        }
    }
}

根据您的评论修改