确认回传OnClientClickbuttonASP.NET

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

而他们都没有工作。

尝试这个:

 <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?"); } 

这里有解决scheme,但我没有看到任何人解释在这里实际发生的事情,所以即使这是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的东西将永远不会被达到,因为“confirm”在返回__doPostBack之前总是返回true / false。

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

你可以把上面的答案放在这样的一行中。 而且你不需要写这个函数。

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

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

代码是这样的:

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

尝试这个 :

 <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="return confirm('Are you sure ?');" 还设置: CausesValidation="False"

尝试这个:

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

确认之前,这是一个简单的方法来做客户端validation。 它利用内置的ASP.NETvalidationJavaScript。

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

来源: 客户端validation使用ASP.Netvalidation控件从Javascript

尝试这个:

 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); } } 

在button调用function:

 <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") { } }