【问题标题】:Confirm postback OnClientClick button ASP.NET确认回发 OnClientClick 按钮 ASP.NET
【发布时间】:2012-12-13 01:06:39
【问题描述】:
<asp:Button runat="server" ID="btnUserDelete" Text="Delete" CssClass="GreenLightButton"
                           OnClick="BtnUserDelete_Click"
                           OnClientClick="return UserDeleteConfirmation();" 
 meta:resourcekey="BtnUserDeleteResource1" />

我试过了:

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

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

    return false;
 }

它们都不起作用。

【问题讨论】:

  • 确保页面验证器没有被触发......如果验证被触发,您将无法提交页面。顺便说一句,代码看起来很糟糕
  • 我知道您找到了解决方案,但我在下面发布了一个答案,解释了此问题的根本原因。

标签: javascript asp.net button postback


【解决方案1】:

试试这个:

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

这样“返回”只在用户点击“取消”时执行,而不是在用户点击“确定”时执行。

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

function UserDeleteConfirmation() {
    return confirm("Are you sure you want to delete this user?");
}

【讨论】:

  • 谢谢)它对我来说很好,唯一剩下的就是找出为什么其他脚本没有
  • 我试过 function UserDeleteConfirmation() { return confirm("你确定要删除这个用户吗?");它不起作用以太
  • 如果更新到较短的UserDeleteConfirmation(),则可以使用OnClientClick="return UserDeleteConfirmation();"
  • @AlexMcMillan - 不,返回可能会停止提交操作。至少在 LinkBut​​ton 中,它确实如此(因为它依赖于进一步的 javascript,这将被忽略)。请参阅 chrismay 的答案。
  • @HansKesting 大声呼喊……微软为什么要这么做?这种愚蠢的想法贯穿于整个asp——无缘无故地使用了多年的标准和模式。微软技术应该被取缔。
【解决方案2】:

这里有一些可行的解决方案,但我没有看到有人解释这里实际发生的事情,所以即使这已经 2 岁了,我也会解释一下。

您添加的 onclientclick javascript 没有任何“错误”。问题是 asp.net 将它添加到 onclick 东西上,以便在您放入其中的任何代码运行之后运行。

例如这个 ASPX:

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

在渲染时变成了这个 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">

如果您仔细观察,将永远无法到达 __doPostBack 内容,因为“确认”将始终在到达 __doPostBack 之前返回 true/false。

这就是为什么你需要让confirm 只返回false 而当值为true 时不返回。从技术上讲,它返回 true 还是 false 都没有关系,这种情况下的任何返回都会产生阻止调用 __doPostBack 的效果,但是为了约定,我会保留它,以便它在 false 时返回 false 并且对 true 不执行任何操作.

【讨论】:

  • 谢谢,这就是让你陷入困境的事情。
【解决方案3】:

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

    <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;"  />

【讨论】:

  • 我在调用函数时在回发期间遇到了javascript错误,但使用这种方法是成功的。
【解决方案4】:

使用 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>

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>

【讨论】:

    【解决方案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"  />
    

    【讨论】:

    • 表单是否回发? “不起作用”是什么意思?
    • 根本不回发=/
    • 可以,但是使用 OnClientClick="UserDeleteConfirmation()" 它在两个选择上都可以很好地回发
    【解决方案6】:

    代码是这样的:

    在 Aspx 中:

    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CausesValidation=true />
    

    在 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";
        }
    }
    

    【讨论】:

    • 服务器验证,而不是客户端验证
    【解决方案7】:

    试试这个: OnClientClick="return confirm('Are you sure ?');" 同时设置:CausesValidation="False"

    【讨论】:

    • CausesValidation="true"OnClientClick 不兼容?
    【解决方案8】:

    试试这个:

    <asp:Button runat="server" ID="btnDelete" Text="Delete"
       onClientClick="javascript:return confirm('Are you sure you want to delete this user?');" OnClick="BtnDelete_Click" />
    

    【讨论】:

      【解决方案9】:

      这是在确认之前进行客户端验证的简单方法。 它利用了内置的 ASP.NET 验证 javascript。

      <script type="text/javascript">
          function validateAndConfirm() {
              Page_ClientValidate("GroupName");  //'GroupName' is the ValidationGroup
              if (Page_IsValid) {
                  return confirm("Are you sure?");
              }
              return false;
          }
      </script>
      
      <asp:TextBox ID="IntegerTextBox" runat="server" Width="100px" MaxLength="6" />
      <asp:RequiredFieldValidator ID="reqIntegerTextBox" runat="server" ErrorMessage="Required"
          ValidationGroup="GroupName"  ControlToValidate="IntegerTextBox" />
      <asp:RangeValidator ID="rangeTextBox" runat="server" ErrorMessage="Invalid"
          ValidationGroup="GroupName" Type="Integer" ControlToValidate="IntegerTextBox" />
      <asp:Button ID="SubmitButton" runat="server" Text="Submit"  ValidationGroup="GroupName"
          OnClick="SubmitButton_OnClick" OnClientClick="return validateAndConfirm();" />
      

      来源:Client Side Validation using ASP.Net Validator Controls from Javascript

      【讨论】:

        【解决方案10】:

        试试这个:

        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
        
                if (confirm("Your asking")) {
                    confirm_value.value = "Yes";
                    document.forms[0].appendChild(confirm_value);
                }
            else {
                confirm_value.value = "No";
                document.forms[0].appendChild(confirm_value);
            }
        }
        

        在按钮调用函数中:

        <asp:Button ID="btnReprocessar" runat="server" Text="Reprocessar" Height="20px" OnClick="btnReprocessar_Click" OnClientClick="Confirm()"/>
        

        类.cs调用方法:

                protected void btnReprocessar_Click(object sender, EventArgs e)
            {
                string confirmValue = Request.Form["confirm_value"];
                if (confirmValue == "Yes")
                {
        
                }
            }
        

        【讨论】:

          【解决方案11】:

          我知道这是旧的,有很多答案,有些真的很复杂,可以快速和内联:

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

          【讨论】:

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