使用 UpdatePanel 时的多个并发回发

发布于 2024-08-14 03:38:38 字数 1087 浏览 2 评论 0原文

这是我为演示我的问题而构建的示例应用程序。一个单独的 aspx 页面,上面有以下内容:

<form id="form1" runat="server">
    <asp:ScriptManager runat="server" />
    <asp:Button runat="server" ID="btnGo" Text="Go" OnClick="btnGo_Click" />
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <asp:TextBox runat="server" ID="txtVal1" />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

然后,在代码隐藏中,我们有以下内容:

protected void btnGo_Click(object sender, EventArgs e)
{
    Thread.Sleep(5000);

    Debug.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToString("HH:MM:ss.fffffff"), txtVal1.Text));

    txtVal1.Text = "";
}

如果您运行此代码并多次单击“Go”按钮,您将在“Output”窗口上看到多个调试语句,显示多个请求已被处理。这似乎与更新面板记录的行为相矛盾(即,如果您在处理更新面板时发出请求,则第一个请求将被终止,当前请求将被处理)。

无论如何,重点是我想解决它。显而易见的选择是使用 Javascript 在第一次按下后禁用按钮,但这让我觉得很难维护,我们可能在很多屏幕上遇到同样的问题,如果有人重命名按钮,它很容易被破坏。

您有什么建议吗?也许我可以在 Global.asax 的 BeginRequest 中做一些事情来检测重复的请求? UpdatePanel 上是否有某些设置或功能可以阻止它执行此操作,或者 AjaxControlToolkit 中是否有某些设置或功能可以阻止它?

Here's an example app that I built to demonstrate my problem. A single aspx page with the following on it:

<form id="form1" runat="server">
    <asp:ScriptManager runat="server" />
    <asp:Button runat="server" ID="btnGo" Text="Go" OnClick="btnGo_Click" />
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <asp:TextBox runat="server" ID="txtVal1" />
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

Then, in code behind, we have the following:

protected void btnGo_Click(object sender, EventArgs e)
{
    Thread.Sleep(5000);

    Debug.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToString("HH:MM:ss.fffffff"), txtVal1.Text));

    txtVal1.Text = "";
}

If you run this and click on the "Go" button multiple times you will see multiple debug statements on the "Output" window showing that multiple requests have been processed. This appears to contradict the documented behaviour of update panels (i.e. If you make a request while one is processing, the first requests gets terminated and the current one is processed).

Anyway, the point is I want to fix it. The obvious option would be to use Javascript to disable the button after the first press, but that strikes me as hard to maintain, we potentially have the same issue on a lot of screens it could be easily broken if someone renames a button.

Do you have any suggestions? Perhaps there is something I could do in BeginRequest in Global.asax to detect a duplicate request? Is there some setting or feature on the UpdatePanel to stop it doing this, or maybe something in the AjaxControlToolkit that will prevent it?

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

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

发布评论

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

评论(2

吃素的狼 2024-08-21 03:38:38

创建一个从 asp Button 继承的自定义控件,并具有用于禁用和启用自身的脚本。以下内容未经测试,但只是一个起点。

public class SafeButton : System.Web.UI.WebControls.Button{
public SafeButton()
{
    OnClientClick = "javascript to disable this button goes here";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "keyforthis", "javascript to enable this button goes here", true);
}

Make a custom control which inherits up from the asp Button and has script to disable and enable itself. The bellow isn't tested buts a starting point.

public class SafeButton : System.Web.UI.WebControls.Button{
public SafeButton()
{
    OnClientClick = "javascript to disable this button goes here";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "keyforthis", "javascript to enable this button goes here", true);
}
甜心小果奶 2024-08-21 03:38:38

第一个解决方案

您可以通过设置(在按钮的标记中)来防止用户进行多次单击,

OnClientClick="this.disabled = true;" 

并设置此属性

UseSubmitBehavior="false"

,这将在单击按钮后禁用该按钮。您不需要启用该按钮,一旦从回发返回,它就会自行启用。
“UseSubmitBehavior”将关闭默认的提交行为并允许提交,尽管按钮被禁用。

上述解决方案仅适用于按钮控件,并非所有平台都支持 请参阅 MSDN 了解支持的平台。


第二种解决方案:

这将提交表单,如果 10 毫秒后该页面在客户端上仍然可用,则按钮\控件将禁用。

编写一个 javascript 函数

<script language="javascript" >

function preventMultipleClicks(a){
   window.setTimeout(function() { a.disabled=true; }, 10)
   return true;
}

</script>

,然后在按钮或回发控件的 OnClientClick 属性中调用它

OnClientClick="preventMultipleClicks(this);"

First Solution

You can prevent the user from making multiple clicks by setting (in the markup of the button)

OnClientClick="this.disabled = true;" 

and set this property

UseSubmitBehavior="false"

This will disable the button once it is clicked. You do not need to enable the button, it will enable itself once it returns from the postback.
The 'UseSubmitBehavior' will turn off the default Submit behavior and allow the submit although the button is disabled.

The above solution only applies to button controls, and is not supported on all platforms see MSDN for supported platforms.


Second solution:

This will submit the form, if the page is still available on the client after 10 milliseconds the button\control will disable.

Write a javascript function

<script language="javascript" >

function preventMultipleClicks(a){
   window.setTimeout(function() { a.disabled=true; }, 10)
   return true;
}

</script>

Then call it in the OnClientClick property of the button or postback control

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