我有一个标准的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 =“ false”添加到deleteButton标记中,但随后它将忽略确认对话框的答案.
所以也许我在这里问太多的ASP.NET.任何想法如何使这项工作?
谢谢,
克雷格
解决方法:
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;}");