javascript 确认 asp.net 按钮永远不会提交?
我有一个常规的 asp: 按钮。我正在 .Net 3.5 中工作。我尝试使用 OnClientClick 属性向按钮添加 js 确认以及添加代码隐藏,结果是相同的。无论用户在确认弹出窗口中单击什么,表单都不会提交?
BtnDeleteSelected.Attributes.Add("onclick", "return recognize('确定要删除吗?');");
出现确认对话框,如果我选择“确定”,它仍然不会提交..有什么想法吗?谢谢。
i have a regular asp:button. I am working in .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. 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?');");
The confirm dialog appears and if i select "OK" it still does not submit.. Any ideas? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果验证通过,是否必须返回 TRUE?意思是,如果confirm()返回false,我认为表单不会提交。
Do you have to return TRUE if validation passes? Meaning, if confirm() returns false I don't think the form will submit.
你的代码表面上看起来没问题。您是否有任何验证器可能会阻止其提交?尝试添加
BtnDeleteSelected.CausesValidation = 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.请尝试调用 doPostBack 函数;)
我的意思是:
希望有帮助,
Please try calling doPostBack function ;)
I mean:
Hope that helps,
那是因为您不应该在所有情况下都返回。如果您查看页面的源代码并查找按钮标记,您可能会看到这一点...
在某些情况下,__doPostback 调用是由 ASP.NET 框架自动插入的。由于无论
confirm
的结果如何,您都会立即返回,因此回发永远不会触发。最终的解决方案是不设置 onclick 属性,而是使用不引人注目的方法。但是,如果有紧急原因需要通过 onclick 在服务器端控制此操作,您可以使用简单的if
语句,并且仅在按下“取消”时返回...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...
The __doPostback invocation is inserted automatically by the ASP.NET framework in some cases. Since you return immediately regardless of the result of
confirm
, the postback never fires. The ultimate solution would be to not to set the onclick attribute, but instead use an unobtrusive approach. However, if there is pressing reason to control this on the server-side via onclick, you can use a simpleif
statement and only return when they press "Cancel"...我也有这个问题。我正在使用在数据网格的
OnItemCreated
代码中添加客户端 JavaScript,如下所示:由于某种原因,这不起作用。然后,我将 JavaScript 移动到设计视图中的
中,如下所示:这对我有用。
I had this problem too. I was using adding the client side javascript in the
OnItemCreated
code of a datagrid like this:For some reason that didn't work. I then moved the JavaScript to the
<asp:linkbutton>
in the design view like this:And that worked for me.