<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;
}
并且它们都不起作用。
答案 0 :(得分:78)
试试这个:
<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?");
}
答案 1 :(得分:33)
这里有一些解决方案可行,但我没有看到有人在这里解释实际发生的事情,所以即使这是2岁,我也会解释它。
没有什么&#34;错误&#34;使用您正在添加的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的东西,因为&#34;确认&#34;在到达__doPostBack之前,它总会返回true / false。
这就是为什么你需要让确认只返回false而不是在值为true时返回。从技术上讲,如果它返回true或false并不重要,这个实例中的任何返回都会阻止调用__doPostBack,但是对于约定我会留下它以便在false时返回false并且什么都不做为了真实。
答案 2 :(得分:14)
您可以将上述答案放在这样的一行中。而且您不需要编写该函数。
<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;" />
答案 3 :(得分:12)
使用jQuery UI对话框:
SCRIPT:
<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>
答案 4 :(得分: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" />
答案 5 :(得分:5)
代码是这样的:
在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";
}
}
答案 6 :(得分:4)
试试这个:
OnClientClick="return confirm('Are you sure ?');"
同时设置:CausesValidation="False"
答案 7 :(得分:3)
试试这个:
<asp:Button runat="server" ID="btnDelete" Text="Delete"
onClientClick="javascript:return confirm('Are you sure you want to delete this user?');" OnClick="BtnDelete_Click" />
答案 8 :(得分:1)
这是在确认之前进行客户端验证的简单方法。 它利用了内置的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
答案 9 :(得分:0)
试试这个:
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")
{
}
}
答案 10 :(得分:0)
我知道这是旧的,有很多答案,有些真的很复杂,可以快速和内联:
<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" />