[英]javascript confirm asp.net button never submits?
i have a regular asp:button. 我有一个常规的asp:button。 I am working in .Net 3.5.
我正在.Net 3.5中工作。 I've tried adding a js confirm to the button with the OnClientClick attribute as well as adding in code-behind and the result is the same.
我试着用OnClientClick属性向按钮添加一个js确认,以及在代码后添加,结果是相同的。 No matter what the user clicks in the confirm pop-up the form will not submit??
无论用户在确认弹出窗口中单击什么,表单都不会提交?
BtnDeleteSelected.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');"); BtnDeleteSelected.Attributes.Add(“ onclick”,“返回确认('确定要删除吗?');”);
The confirm dialog appears and if i select "OK" it still does not submit.. Any ideas? 出现确认对话框,如果我选择“确定”,它仍然不会提交。 thanks.
谢谢。
That's because you should not be returning in all cases. 那是因为您不应该在所有情况下都返回。 If you view the source of the page and look for the button markup, you are likely to see this...
如果查看页面的源代码并查找按钮标记,则可能会看到此...
onclick="return confirm('Ok?'); __doPostback();"
The __doPostback invocation is inserted automatically by the ASP.NET framework in some cases. 在某些情况下,ASP.NET框架会自动插入__doPostback调用。 Since you return immediately regardless of the result of
confirm
, the postback never fires. 由于无论
confirm
结果如何,您都会立即返回,因此永远不会触发回发。 The ultimate solution would be to not to set the onclick attribute, but instead use an unobtrusive approach . 最终的解决方案是不设置onclick属性,而是使用不引人注目的方法 。 However, if there is pressing reason to control this on the server-side via onclick, you can use a simple
if
statement and only return when they press "Cancel"... 但是,如果迫切需要通过onclick在服务器端对此进行控制,则可以使用简单的
if
语句,并且仅在他们按下“取消”时才返回...
string js = "if(!confirm('Are you sure you want to delete?')) return false;";
BtnDeleteSelected.Attributes.Add("onclick", js);
I had this problem too. 我也有这个问题。 I was using adding the client side javascript in the
OnItemCreated
code of a datagrid like this: 我正在使用在数据网格的
OnItemCreated
代码中添加客户端javascript,如下所示:
myTableCell = e.Item.Cells(iDeleteColumnNumber) 'Delete Button Column
myDeleteButton = myTableCell.Controls(1)
If myDeleteButton.UniqueID = "lbtnDelete" Then
myDeleteButton.Attributes.Add("onclick", "if(!confirm('OK to Delete?'))return false;")
End If
For some reason that didn't work. 由于某些原因,该方法不起作用。 I then moved the JavaScript to the
<asp:linkbutton>
in the design view like this: 然后,我将JavaScript移到设计视图中的
<asp:linkbutton>
,如下所示:
<asp:LinkButton id="lbtnDelete" runat="server" CssClass="link-text" Text="<img border=0 src=../images/icons/icon-delete.gif alt=Delete>"
CommandName="Delete" CausesValidation="false" OnClientClick="if(!confirm('OK to Delete?'))return false;"></asp:LinkButton>
And that worked for me. 这对我有用。
Do you have to return TRUE if validation passes? 如果验证通过,您是否必须返回TRUE? Meaning, if confirm() returns false I don't think the form will submit.
意思是,如果confirm()返回false,我认为表格不会提交。
Your code looks OK on the surface. 您的代码表面上看起来不错。 Do you have any validators that might be preventing it being submitted?
您是否有任何验证者可能阻止其提交? Try adding
BtnDeleteSelected.CausesValidation = false
to prevent the delete button calling any client-side validators. 尝试添加
BtnDeleteSelected.CausesValidation = false
以防止删除按钮调用任何客户端验证器。
Please try calling doPostBack function ;) 请尝试调用doPostBack函数;)
I mean: 我的意思是:
BtnDeleteSelected.Attributes.Add("onclick", "CheckConfirm();");
<script language="javascript">
function CheckConfirm()
{
if ( confirm('Are you sure you want to delete?') )
__doPostBack('BtnDeleteSelected','');
else
return false;
return true;
}
</script>
Hope that helps, 希望能有所帮助,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.