javascript 确认 asp.net 按钮永远不会提交?

发布于 2024-09-19 12:23:55 字数 246 浏览 6 评论 0原文

我有一个常规的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如何视而不见 2024-09-26 12:23:56

如果验证通过,是否必须返回 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.

谷夏 2024-09-26 12:23:56

你的代码表面上看起来没问题。您是否有任何验证器可能会阻止其提交?尝试添加 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.

长安忆 2024-09-26 12:23:56

请尝试调用 doPostBack 函数;)

我的意思是:

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>

希望有帮助,

Please try calling doPostBack function ;)

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,

薄暮涼年 2024-09-26 12:23:55

那是因为您不应该在所有情况下都返回。如果您查看页面的源代码并查找按钮标记,您可能会看到这一点...

onclick="return confirm('Ok?'); __doPostback();"

在某些情况下,__doPostback 调用是由 ASP.NET 框架自动插入的。由于无论 confirm 的结果如何,您都会立即返回,因此回发永远不会触发。最终的解决方案是不设置 onclick 属性,而是使用不引人注目的方法。但是,如果有紧急原因需要通过 onclick 在服务器端控制此操作,您可以使用简单的 if 语句,并且仅在按下“取消”时返回...

string js = "if(!confirm('Are you sure you want to delete?')) return false;";
BtnDeleteSelected.Attributes.Add("onclick", js);

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. 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 simple if statement and only return when they press "Cancel"...

string js = "if(!confirm('Are you sure you want to delete?')) return false;";
BtnDeleteSelected.Attributes.Add("onclick", js);
爱的故事 2024-09-26 12:23:55

我也有这个问题。我正在使用在数据网格的 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

由于某种原因,这不起作用。然后,我将 JavaScript 移动到设计视图中的 中,如下所示:

   <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>

这对我有用。

I had this problem too. I was using adding the client side javascript in the OnItemCreated code of a datagrid like this:

 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:

   <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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文