【问题标题】:Clicking another button before one’s action is complete在操作完成之前单击另一个按钮
【发布时间】:2013-06-17 14:55:26
【问题描述】:

我有一个有两个按钮的 asp.net 网页。它们驻留在两个不同的update panels 中并更新两个不同的文本框。

我首先点击了第一个按钮,等待了 5 秒,然后点击了第二个按钮。结果是我期望两个文本框都有价值——但只有一个有。

问题

  1. 为什么第一个文本框没有更新?当我单击第二个按钮时,第一个请求是否被中止?有任何 msdn 参考吗?
  2. 有什么方法可以在两个文本框中获取值,即使我们在第一个完成之前单击第二个按钮?

标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest.aspx.cs" Inherits="MyUI.AjaxTest"
EnableSessionState="False" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scrSubscribers" runat="server" />
<div>
    <div style="border-style: solid; border-color: #00FFFF">
        <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <div class="firstTextBox">
                    <asp:TextBox ID="txtAssociateName" runat="server" Width="150px" MaxLength="81" TabIndex="1"></asp:TextBox>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <div class="firstTextBox">
                    <asp:TextBox ID="txtDepartment" runat="server" Width="150px" MaxLength="81" TabIndex="1"></asp:TextBox>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    <br />
    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load" UpdateMode="Conditional">
        <ContentTemplate>
            <div id="searchValues" class="searchValuesDiv">
                <div id="searchLine1" class="searchLine">
                </div>
                <div id="searchLine5" class="searchLine">
                    <div class="btnSearchDiv">
                        <asp:Button ID="btnFirst" class="btnSearch" runat="server" OnClick="SearchSubscribersClick"
                            Text="FirstButton" ValidationGroup="Search" />
                        <asp:TextBox ID="txtFirstDate" runat="server" Width="150px" MaxLength="81" TabIndex="1"></asp:TextBox>
                    </div>
                </div>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <br />
    <br />
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" OnLoad="UpdatePanel1_Load" UpdateMode="Conditional">
        <ContentTemplate>
            <div id="Div1" class="searchValuesDiv">
                <div id="Div3" class="searchLine">
                    <div class="btnSearchDiv">
                        <asp:Button ID="btnSecond" class="btnSearch" runat="server" OnClick="SecondClick"
                            Text="SecondButton" ValidationGroup="Search" />
                        <asp:TextBox ID="txtSecondDate" runat="server" Width="150px" MaxLength="81" TabIndex="1"></asp:TextBox>
                    </div>
                </div>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:HiddenField ID="hdnSearchClickStatus" runat="server" Value="Blank Value" />
</div>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/ExpandableSearchResultScript.js" type="text/javascript"></script>
</form>
</body>
</html>

代码隐藏

    protected void SearchSubscribersClick(object sender, EventArgs e)
    {
        Thread.Sleep(10000);
        txtAssociateName.Text = "A_"+ DateTime.Now.ToString();
    }

    protected void SecondClick(object sender, EventArgs e)
    {
        Thread.Sleep(10000);
        txtDepartment.Text = "B_" + DateTime.Now.ToString();
    }

结果

【问题讨论】:

标签: asp.net ajax asp.net-ajax


【解决方案1】:

当发出新请求时,任何 AJAX 请求都会被取消。这不是来自 MSDN。这就是 AJAX 请求的工作方式。

【讨论】:

  • “这不是来自 MSDN。这就是 AJAX 请求的工作方式” 甚至 ASP.NET Ajax 也在 MSDN 上:msdn.microsoft.com/en-us/library/bb398874(v=vs.100).aspx
  • 确实如此。我的意思是:此功能并非特定于 Microsoft 开发的技术,因此无需搜索 MSDN,因为这是标准 Ajax 行为。
【解决方案2】:

答案可以从问题ASP.NET AJAX using UpdatePanels得出

@Sergey 评论:

来自 UpdatePanel 的请求仍然贯穿整个页面生命周期。因此,每个请求都有相关的状态数据。这就是为什么两个请求不能为同一个会话同时运行的原因。 Jquery ajax 请求命中无状态静态方法。

@Dave Ward 回答:

UpdatePanel 的异步回发仍然只是:回发。如果允许并行发生多个回发,它们的新 ViewState 通常会彼此不同步。然后,下一次回发将引发 ViewState 验证错误和/或在第三次回发完成后,其中一个回发对页面状态的修改将丢失。

另一方面,页面方法和 Web 服务调用不受 WebForms ViewState 或页面生命周期的阻碍。因此,在浏览器的同时请求限制允许的情况下,其中许多可能并行发生(通常为 4-8,但在某些浏览器如 IE6 中只有 2 个)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 2011-10-31
    • 2021-10-26
    相关资源
    最近更新 更多