0

我创建了一个函数,每次单击动态创建的 ImageButton 时都会调用它。但是 precenDefault() 不起作用。该功能正在工作,除了阻止默认值。

jQuery

<script type="text/javascript">
    function EmpDelete(event, id) {
        event.preventDefault();
        $("#bName").text(id);        
        $('#dialog').dialog({            
            title: "Delete Employee?",
            modal: true,
            width: 500,
            height: 250,
            buttons: {
                "Cancel": function () {
                    $(this).dialog("close");
                    return false;
                },
                "Continue": function () {
                    $(this).dialog("close");
                    return true;
                }
            }
        });        
    };    
</script>

ASPX.CS

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
        e.Row.Cells[iDelete].Controls.Add
            (new ImageButton
            {
                ImageUrl = "Images/database_delete.png",
                CommandArgument = e.Row.RowIndex.ToString(),
                CommandName = "Delete",
                ID = "imgDelete",
                CssClass = "imgDelete",
                OnClientClick = "return EmpDelete(" + e.Row.Cells[iFNmae].Text + ");"
            });
    }
}
4

3 回答 3

0

第一个错误是您不能以preventDefault这种方式调用该函数。您设置呼叫的方式最好在呼叫结束时返回 false ,这样单击只会打开对话框而不会继续;

现在按钮将在该函数已经返回到 ImageButton 之后运行,因为对话框只是打开并返回 - 不是等待按钮返回OKCancel

<script type="text/javascript">
    function EmpDelete(event, id) 
    {
        // this is not correct because you do not have the event for preventDefault
        // event.preventDefault();
        $("#bName").text(id);        
        $('#dialog').dialog({            
            title: "Delete Employee?",
            modal: true,
            width: 500,
            height: 250,
            // that buttons here will not work as you belive !
            // they are not return and let the post back occurs after the event.
            buttons: {
                "Cancel": function () {
                    $(this).dialog("close");
                    return false;
                },
                "Continue": function () {
                    $(this).dialog("close");
                    return true;
                }
            }
        });        
      // you need to return here false.
      return false;
    };    
</script>

这里的解决方案是保存 post back click,并在对话框关闭时使用它。

它甚至更好,真的很容易,只需返回confirm

OnClientClick = "return confirm('are you sure to delete this employee ?')'

甚至更好的方法是在使用控件的类时吸引该确认,而不是将其添加到后面的代码中。

于 2013-05-16T10:15:58.833 回答
0

事件需要传递给函数。该函数需要两个参数,但它只传递一个。

 OnClientClick = "return EmpDelete(e," + e.Row.Cells[iFNmae].Text + ");"

完整来源

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
        e.Row.Cells[iDelete].Controls.Add
            (new ImageButton
            {
                ImageUrl = "Images/database_delete.png",
                CommandArgument = e.Row.RowIndex.ToString(),
                CommandName = "Delete",
                ID = "imgDelete",
                CssClass = "imgDelete",
                OnClientClick = "return EmpDelete(e" + e.Row.Cells[iFNmae].Text + ");"
            });
    }
}
于 2013-05-16T09:56:59.713 回答
0

我通过使用__doPostBack(uniqueID, '');jQuery“继续”按钮解决了这个问题

在我添加的 Page_Load() 事件上

 if (!Page.IsPostBack)
    {
        ClientScript.GetPostBackEventReference(gvEmployee, string.Empty);
    }

我在 Gridview_RowDataBound 事件上调用了 jQuery 对话框

e.Row.Cells[iDelete].Controls.Add
            (new ImageButton
            {
                ImageUrl = "Images/database_delete.png",
                CommandArgument = e.Row.RowIndex.ToString(),
                CommandName = "Delete",
                ID = "imgEmpDelete" + e.Row.Cells[iID].Text,
                **OnClientClick = "javascript:return rowAction(this.name)",**
                ClientIDMode = System.Web.UI.ClientIDMode.Static
            });

希望它可以帮助任何人。

于 2013-05-17T23:40:59.480 回答