usesubmitbehaviour 设置为 false 时两个 asp:button 控件的确认弹出窗口

发布于 2024-08-19 01:57:37 字数 954 浏览 5 评论 0原文

我有一个嵌入在 asp.net (2.0) 页面中的控件。当控件位于页面上时,我想要按返回键在控件上运行操作时的默认行为,而不是页面上的任何内容。为此我发现

UseSubmitBehaviour="false"

页面上按钮的设置效果很好。这很好,直到我需要在单击页面上的此按钮时出现确认窗口。当他们按下该按钮时,他们将从系统中永久删除记录,并希望确保他们单击了正确的按钮。该按钮如下所示:

<asp:Button runat="server" ID="btnDeleteCollationResultstop" Text="Delete Selected" UseSubmitBehavior="true"
ToolTip="Delete all the selected items in the Collation tool"  OnClick="btnDeleteCollationResults_Click" 
OnClientClick="return confirm('Are you sure you wish to Delete the selected Item(s)?');" />

如果我将 UseSubmitBehaviour 设置为 true,弹出确认窗口会加载并工作,但返回键会触发删除按钮,这又是我真的不这样做。不想要。

所以我的问题是如何让它执行以下操作:

  1. 使返回键的默认操作在我的控件上触发,而不是在其所在的页面上触发

  2. 确保当用户单击删除按钮时出现确认窗口,并且仅在确认删除时才继续?

谢谢乔恩

编辑

我刚刚的一个想法是让所有按钮都不会在返回时提交回来,并挂钩一些JQuery来监听Return并为我想要的按钮进行提交, (但不知道如何立即做到这一点)。

I have a control embedded in an asp.net (2.0) page. When the control is on a page I want the default behaviour when the return key is press to run an action on the control, not anything on the page. To this end I found that setting

UseSubmitBehaviour="false"

on the buttons on the page worked great. This was fine until I needed to have a confirmation window appear when this button on the page was clicked. When they push that button they are permanently deleting records from the system and want to make sure they are clicking the correct button. The button looks like this:

<asp:Button runat="server" ID="btnDeleteCollationResultstop" Text="Delete Selected" UseSubmitBehavior="true"
ToolTip="Delete all the selected items in the Collation tool"  OnClick="btnDeleteCollationResults_Click" 
OnClientClick="return confirm('Are you sure you wish to Delete the selected Item(s)?');" />

The popup confirmation window does load and work if I set UseSubmitBehaviour to true but then the return key fires the delete button, which again I really don't want.

So my question is how can I get it to do the following:

  1. make default action of the return key to fire on my control and not the page it is in

  2. ensure a confirmation window appears when the user clicks the delete button and only proceed if they confirm the deletion?

Thanks

Jon

EDIT

One idea I just had would be to make it so that none of the buttons submit back on return, and hook in some JQuery to listen for Return and do the submit for the button I want, (don't know how to do that straight away though).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏有森光若流苏 2024-08-26 01:57:37

这里发生的情况是,当您在按钮中设置 UseSubmitBehavior="true" 时,该按钮将呈现为 .这使用了按下回车按钮时提交表单的浏览器默认行为。但如果设置 UseSubmitBehavior="false",但按钮呈现为 ;并且,onclick 事件(在客户端中)连接到提交表单的“__doPostBack()”。

问题是,如果您向 OnClientClick 属性添加任何内容,它会在 __doPostBack() 之前运行。如果返回 true 或 false,它将结束 onclick 事件的处理,因为返回了一个值,并且 __doPostBack() 永远不会被触发。呈现的标签如下所示:

<input type="button" name="button1" value="Click Me" onclick="return confirm('are you sure?');__doPostBack('button1','')" id="button1" />

AjaxControlToolkit 有一个控件,如果您想使用它,它可以实现此功能。它是ConfirmButton控件(http://www.asp.net/ AJAX/AjaxControlToolkit/Samples/ConfirmButton/ConfirmButton.aspx)。

如果您想使用 jQuery,这里有一个示例,其功能与ConfirmButton 控件基本相同。

您的按钮应该看起来像这样(一定要删除 OnClientClick 属性):

<asp:Button ID="button1" runat="server" Text="Click Me" UseSubmitBehavior="false" onclick="button1_Click" />

这是 JavaScript 代码:

<script type="text/javascript">
    $(function() {
        var btn = $('#<%= button1.ClientID %>');
        var oldScript = btn.attr('onclick');
        btn.attr('onclick', '');
        btn.click(function() {
            if (confirm('click a button.')) {
                if (oldScript) {
                    if ($.isFunction(oldScript)) {
                        oldScript();
                    } else {
                        eval(oldScript);
                    }
                }
                return true;
            } else {
                return false;
            }
        });
    });
</script>

基本上,它的作用是删除当前的“onclick”属性值并将其存储在临时变量中。然后它将确认连接到“单击”事件。如果confirm返回true,它将执行原来在“onclick”中的旧脚本。如果确认返回 false,则不会发生其他情况。

What's happening here is that when you set UseSubmitBehavior="true" in your button, the button is rendered as . This uses the browser default behavior of submitting the form when the enter button is pressed. But if you set UseSubmitBehavior="false", but button is rendered as ; and, the onclick event (in the client) is wired up to '__doPostBack()' which submits the form.

The problem is that if you add anything to the OnClientClick property, it gets run before the __doPostBack(). If you return true or false, it will end the processing of the onclick event, because a value was returned, and __doPostBack() never gets fired. Here's what the rendered tag looks like:

<input type="button" name="button1" value="Click Me" onclick="return confirm('are you sure?');__doPostBack('button1','')" id="button1" />

The AjaxControlToolkit has a control which will make this work, if you'd like to use that. It is the ConfirmButton control (http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ConfirmButton/ConfirmButton.aspx).

If you'd like to use jQuery, here's an example that does basically the same thing as the ConfirmButton control.

You're button should look something like this (be sure to remove the OnClientClick property):

<asp:Button ID="button1" runat="server" Text="Click Me" UseSubmitBehavior="false" onclick="button1_Click" />

And here's the JavaScript code:

<script type="text/javascript">
    $(function() {
        var btn = $('#<%= button1.ClientID %>');
        var oldScript = btn.attr('onclick');
        btn.attr('onclick', '');
        btn.click(function() {
            if (confirm('click a button.')) {
                if (oldScript) {
                    if ($.isFunction(oldScript)) {
                        oldScript();
                    } else {
                        eval(oldScript);
                    }
                }
                return true;
            } else {
                return false;
            }
        });
    });
</script>

Basically what this does is it removes the current "onclick" attribute value and stores it in a temporary variable. Then it wires up the confirm to the "click" event. If the confirm returns true, it will execute the old script that was in the "onclick" originally. If the confirm returns false, nothing else happens.

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