如何确认,然后在asp.net/javascript禁用按钮
-
20-08-2019 - |
题
我有在其上的删除按钮标准ASP.NET 2.0网页。当用户按下删除按钮确认对话框弹出窗口,询问用户我需要什么,不能弄清楚如何拉断是“你确定吗?”。如果用户说是,那么我想禁用删除按钮并执行将运行在服务器端代码deleteButton_Click回发。
下面是标记:
<asp:Button ID="deleteButton" Text="Delete" OnClick="deleteButton_Click" runat="server" />
下面是JavaScript(jQuery中)来处理客户端侧点击:
var deleteButton = $(input.eq(0));
deleteButton.click( function()
{
var a = confirm( "Are you sure you want to delete this item?" );
if ( a == false )
{
return false;
}
else
{
deleteButton.attr( "disabled", "disabled" );
__doPostBack( deleteButton.attr( "id" ), "" );
}
} );
在确认对话框按预期工作和禁用工作也没关系。该格式做回发不错,但它不运行deleteButton_Click事件处理程序。该__doPostBack的JavaScript代码不存在于页面上。
我可以添加UseSubmitBehavior =“假”的deleteButton标签,但那就忽略确认对话框的答案。
所以,也许我要求太多了ASP.NET这里。任何想法如何使这项工作?
谢谢,
克雷格
解决方案 3
感谢您的反馈,但它是一个相当简单的解决方案。
在JavaScript的线应该是:
__doPostBack( deleteButton.attr( "name" ), "" );
,而不是:
__doPostBack( deleteButton.attr( "id" ), "" );
我不知道,“名称”属性是doPostBack方法一直在寻找的人。
其他提示
btnSubmit.Attributes.Add("onclick", "if(confirm(\"Are you sure you want to delete this item?\" ){this.disabled=true;" + ClientScript.GetPostBackEventReference(btnSubmit, "").ToString() + "}else{return false;}");
看看这个StackOverflow的问题,我发现它非常有用:
上表单提交禁用按钮
添加确认逻辑...你应该设置...
我去了一个头,写了一个扩展方法适合你,我还有:)
public static void ConfirmThenDisableButtonOnClick(this Button buttonControl, string confirmMessage, string clientFunction)
{
StringBuilder sb = new StringBuilder();
// If the page has ASP.NET validators on it, this code ensures the
// page validates before continuing.
if (buttonControl.CausesValidation && !String.IsNullOrEmpty(buttonControl.ValidationGroup))
{
sb.Append("if ( typeof( Page_ClientValidate ) == 'function' ) { ");
sb.AppendFormat(@"if ( ! Page_ClientValidate('{0}') ) {{ return false; }} }} ", buttonControl.ValidationGroup);
}
//pop confirm, if confirm==true, disable button
sb.AppendFormat("if(confirm('{0}\')){{this.disabled=true;", confirmMessage.Replace("\"","").Replace("'",""));
// If a secondary JavaScript function has been provided, and if it can be found,
// call it. Note the name of the JavaScript function to call should be passed without
// parens.
if (!String.IsNullOrEmpty(clientFunction))
{
sb.AppendFormat("if ( typeof( {0} ) == 'function' ) {{ {0}() }};", clientFunction);
}
// GetPostBackEventReference() obtains a reference to a client-side script function
// that causes the server to post back to the page (ie this causes the server-side part
// of the "click" to be performed).
sb.Append(buttonControl.Page.ClientScript.GetPostBackEventReference(buttonControl, ""));
// if confirm==false do nothing
sb.Append(";}else{return false;}");
// Add the JavaScript created a code to be executed when the button is clicked.
buttonControl.Attributes.Add("onclick", sb.ToString());
}
刚添加有效性检查,以及:)
而不是通过JavaScript做回发,你可以使用的OnClientClick来完成同样的事情。
<asp:Button OnClientClick="if (confirm('Are you sure?')) {this.disabled = true;} else {return false;}" />
或者,如果你想比单纯禁用按钮执行通过JavaScript更多的行动,可以说是这样的:
<asp:Button OnClientClick="return DisableOnConfirm();" />
您只需要确保你的措辞它return语句的形式,这样,当你不希望ASP功能来运行,该语句将最终说“返回false;”
。使用Javascript:
deleteButton.disabled = true;
C#:
deleteButton.Enabled = false;
您或许应该禁用按钮上的deleteButton_Click事件的服务器端(因为你会做回传,让你重新创建页)。
<asp:Button ID="deleteButton" Text="Delete" OnClick="deleteButton_Click" runat="server" OnClientClick="javascript:return confirm('are you sure blah blah blah ...')" />
和在代码隐藏:
private void deleteButton_Click(object sender, EventArgs e) {
deleteButton.Enabled = false;
// and the rest of event handling ...
}
这是一个有点奇怪的是,点击事件不会开火。我就已经猜到了该按钮是由于被禁止,但通常,只有当按钮在服务器上禁用,不只是客户发生。虽然不是在你的Page_Load功能的理想解决方案,你可以检查一个回传和/或异步回发,并确定(在这种情况下,是你的按钮)回发的目标,然后从那里调用一些方法。还可以传递在使用__doPostBack()函数参数。
首先,我不知道,如果你在谈论客户端点击事件或服务器端单击事件。我想你应该明确地指出,在服务器端单击事件不点火。
总之,尝试<%@页EnableEventValidation =“假”%>您的ASPX页面上,看看它的工作原理(这是默认设置为true)。我不记得这个属性作品的基本事实,但此功能可以确保在服务器端事件只在合法的情况下被解雇。例如。假设你的页面有XSS漏洞和黑客注入的JavaScript调用删除按钮。此属性有助于防止这些攻击。
$(":submit").click(function ()
{
$(this).attr("disabled", true); // disable input
if (confirm("Press OK to submit"))
{
__doPostBack(this.id, ""); // if user presses OK; submit
}
else
{
// If user presses cancel; enable input and stop submit
$(this).attr("disabled", false);
return false;
}
});
我决定使这个JS函数,可以从任一按钮被调用。
下面是一个可行的样品,然而,我会移动JavaScript以不同的地方,例如一个现有js文件或头部标记。
<script>
function confirmPostback(button) {
if (confirm('Are you sure?')) {
button.disabled = true;
__doPostBack(button.name, '')
return true;
} else {
return false;
}
}
</script>
<asp:Button ID="TestPostback" runat="server" Text="Test Postback"
UseSubmitBehavior="false" OnClientClick="return confirmPostback(this)" />