确认回发OnClientClick按钮ASP.NET

mak*_*mbi 45 javascript asp.net postback button

<asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton"
                           OnClick="BtnUserDelete_Click"
                           OnClientClick="return UserDeleteConfirmation();" 
 meta:resourcekey="BtnUserDeleteResource1" />
Run Code Online (Sandbox Code Playgroud)

我试过了:

function UserDeleteConfirmation() {
        if (confirm("Are you sure you want to delete this user?"))
            return true;
        else
            return false;
}
Run Code Online (Sandbox Code Playgroud)

function UserDeleteConfirmation() {
    if (confirm("Are you sure you want to delete this user?")) {
            __doPostBack(btnUserDelete, '');
    }

    return false;
 }
Run Code Online (Sandbox Code Playgroud)

而且它们都不起作用.

Han*_*ing 82

试试这个:

<asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton"
                       OnClick="BtnUserDelete_Click"
                       OnClientClick="if ( ! UserDeleteConfirmation()) return false;" 
 meta:resourcekey="BtnUserDeleteResource1" />
Run Code Online (Sandbox Code Playgroud)

这样,"返回"仅在用户单击"取消"时执行,而不是在他单击"确定"时执行.

顺便说一句,您可以将UserDeleteConfirmation函数缩短为:

function UserDeleteConfirmation() {
    return confirm("Are you sure you want to delete this user?");
}
Run Code Online (Sandbox Code Playgroud)

  • @HansKesting大声呐喊...微软为什么要这么做?这种愚蠢的想法贯穿于整个asp中-打破了无缘无故使用了多年的标准和模式。Microsoft技术应被禁止。 (3认同)

chr*_*may 36

这里有解决方案可行,但我没有看到有人在这里解释实际发生的事情,所以即使这是2年,我也会解释它.

你正在添加的onclientclick javascript没有任何"错误".问题是asp.net正在添加onclick的东西,以便在你运行的任何代码之后运行.

所以例如这个ASPX:

<asp:Button ID="btnDeny" runat="server" CommandName="Deny" Text="Mark 'Denied'" OnClientClick="return confirm('Are you sure?');" />
Run Code Online (Sandbox Code Playgroud)

在呈现时变为此HTML:

<input name="rgApplicants$ctl00$ctl02$ctl00$btnDeny" id="rgApplicants_ctl00_ctl02_ctl00_btnDeny" 
onclick="return confirm('Are you sure?');__doPostBack('rgApplicants$ctl00$ctl02$ctl00$btnDeny','')" type="button" value="Mark 'Denied'" abp="547">
Run Code Online (Sandbox Code Playgroud)

仔细观察,永远不会达到__doPostBack的东西,因为在达到__doPostBack之前,"confirm"将始终返回true/false.

这就是为什么你需要让确认只返回false而不是在值为true时返回.从技术上讲,如果它返回true或false并不重要,这个实例中的任何返回都会阻止调用__doPostBack,但是对于约定我会留下它以便在false时返回false并且不执行任何操作.


Pra*_*tta 15

您可以将上述答案放在这样的一行中.而且您不需要编写该函数.

    <asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton"
         OnClick="BtnUserDelete_Click" meta:resourcekey="BtnUserDeleteResource1"
OnClientClick="if ( !confirm('Are you sure you want to delete this user?')) return false;"  />
Run Code Online (Sandbox Code Playgroud)


Kap*_*wal 12

使用jQuery UI对话框:

脚本:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script>
 $(function () {

            $("#<%=btnUserDelete.ClientID%>").on("click", function (event) {
                event.preventDefault();
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        Ok: function () {
                            $(this).dialog("close");
                            __doPostBack($('#<%= btnUserDelete.ClientID %>').attr('name'), '');
                        },
                        Cancel: function () {
                            $(this).dialog("close");
                        }
                    }
                });
            });
 });
</script>
Run Code Online (Sandbox Code Playgroud)

HTML:

<div id="dialog-confirm" style="display: none;" title="Confirm Delete">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Are you sure you want to delete this user?</p>
</div>
Run Code Online (Sandbox Code Playgroud)


Mah*_*hat 5

试试这个 :

<asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton" 
   onClientClick=" return confirm('Are you sure you want to delete this user?')" 
   OnClick="BtnUserDelete_Click"  meta:resourcekey="BtnUserDeleteResource1"  />
Run Code Online (Sandbox Code Playgroud)


Rck*_*kLN 5

代码是这样的:

在Aspx中:

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CausesValidation=true />
Run Code Online (Sandbox Code Playgroud)

在Cs中:

protected void Page_Load(object sender, System.EventArgs e)
{
     if (!IsPostBack)
     {
         btnSave.Attributes["Onclick"] = "return confirm('Do you really want to save?')";          
     }
}

protected void btnSave_Click(object sender, EventArgs e){
    Page.Validate();
    if (Page.IsValid)
    {
       //Update the database
         lblMessage.Text = "Saved Successfully";
    }
}
Run Code Online (Sandbox Code Playgroud)