【问题标题】:Problems using UpdateProgress使用 UpdateProgress 的问题
【发布时间】:2011-03-20 02:14:54
【问题描述】:

我的 ASP.NET 页面上有一个按钮,它从我的数据库中获取一些数据并将其显示在网格视图上。

这个过程需要一段时间,所以我想我会添加一个 updateprogress AJAX 控件。现在,当我单击按钮时,会显示 updateprogress 图像,并且正在成功地从我的数据库中获取数据(我从数据库中的一些日志中检查了这一点)。但是有两个问题:

(1) updateprogress 图像仅显示大约 2 分钟。但是我的 buttonclick 事件大约需要 5 分钟才能完成。基本上 updateprogress 甚至在我的任务完成之前就停止显示,这违背了它的目的。

(2) GridView 不显示。如果我不使用 scriptmanager/AJAX,它会正确显示。

有什么想法吗?

一些相关代码sn-ps:

<div style="position: absolute; top: 300px; left: 19px; width: 568px; height: 48px;">
    <table>
        <tr>
        <td>
    <asp:Button ID="btnGenerateReport" runat="server" Height="37px" Text="Generate Report" 
        Width="132px" onclick="btnGenerateReport_Click" />
        </td>
        <td class="style5">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server" id="Panel">
    <ContentTemplate>
            <asp:UpdateProgress ID="PageUpdateProgress" runat="server">
                <ProgressTemplate>
                    <img runat="server" src="updateprogress.gif" 
                        style="position: static; width: 32px;"> </img></ProgressTemplate>
            </asp:UpdateProgress>
            <div style="position: absolute; top: 423px; left: 9px; width: 795px; height: 984px;">
        <asp:GridView ID="Report" runat="server" 
            AllowSorting="True" AutoGenerateColumns="False" 
            Height="622px" BorderStyle="Solid" 
            Width="779px" PageSize="100" HorizontalAlign="Center">
            <Columns>
                <asp:BoundField HeaderText="AccountId" DataField="AccountId">
                <ControlStyle BorderStyle="Solid" Width="20px" />
                </asp:BoundField>
                <asp:BoundField HeaderText="User Name" ReadOnly="True" DataField="UserName">
                <ControlStyle Width="50px" />
                </asp:BoundField>
                <asp:BoundField HeaderText="C2" ReadOnly="True" 
                    DataField="C2">
                <ControlStyle BorderStyle="Solid" Width="20px" />
                </asp:BoundField>
                <asp:BoundField HeaderText="C1" ReadOnly="True" 
                    DataField="C1">
                <ControlStyle BorderStyle="Solid" Width="20px" />
                </asp:BoundField>
            </Columns>
        </asp:GridView>
    </div>
    </ContentTemplate>
    <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnGenerateReport" EventName="click" />
    </Triggers>
    </asp:UpdatePanel>
        </td>
        <td>
        <asp:Button ID="btnExportToExcel" runat="server" Text="Export To Excel" 
                Height="37px" Width="132px" onclick="btnExportToExcel_Click" />
        </td>
        </tr>
    </table>
    </div>

代码文件部分:

protected void btnGenerateReport_Click(object sender, EventArgs e)
        {

            FetchAccounts();
            long startId = FetchAuditData();
            AggregateAuditData(startId);
            dsAnalysisReport = GenerateDataSet();
            if (dsAnalysisReport == null || dsAnalysisReport.Tables.Count == 0)
                lblErrorText.Text = "No Data Found for the input information";
            else
                BindData(dsAnalysisReport);
        }

        public void FetchAccounts()
        {
            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open();
                cmdstring = @"[spo_FetchAccounts]";
                cmd = new SqlCommand(cmdstring, sqlConnection) { CommandType = CommandType.StoredProcedure };
                cmd.CommandTimeout = 100000;
                cmd.Parameters.Add("@MaxActivationDate", SqlDbType.DateTime);
                cmd.Parameters["@MaxActivationDate"].Value =
                    DateTime.Parse(txtStartDate.Text) - new TimeSpan(1, 0, 0, 0);
                cmd.ExecuteNonQuery();
                sqlConnection.Close();
            }
        }

        public long FetchAuditData()
        {
            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open(); 
                cmdstring = @"[spo_GetComparisonData]";
                cmd = new SqlCommand(cmdstring, sqlConnection) { CommandType = CommandType.StoredProcedure };
                cmd.CommandTimeout = 100000;
                cmd.Parameters.Add("@StartDate", SqlDbType.DateTime);
                cmd.Parameters["@StartDate"].Value = txtStartDate.Text;
                cmd.Parameters.Add("@EndDate", SqlDbType.DateTime);
                cmd.Parameters["@EndDate"].Value = txtEndDate.Text;
                SqlParameter returnValue = new SqlParameter("@Return_Value", SqlDbType.BigInt);
                returnValue.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(returnValue);
                cmd.ExecuteNonQuery();
                long startId = long.Parse(cmd.Parameters["@Return_Value"].Value.ToString());
                sqlConnection.Close();
                return startId;
            }
        }

        private void AggregateAuditData(long startId)
        {
            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open();
                cmdstring = @"[spo_ComparisonTable]";
                cmd = new SqlCommand(cmdstring, sqlConnection) { CommandType = CommandType.StoredProcedure };
                cmd.CommandTimeout = 100000;
                cmd.Parameters.Add("@StartId", SqlDbType.Int);
                cmd.Parameters["@StartId"].Value = startId;
                cmd.ExecuteNonQuery();
                sqlConnection.Close();
            }
        }


        public DataSet GenerateDataSet()
        {

            using (var sqlConnection = new SqlConnection(ConnectionString))
            {
                sqlConnection.Open();
                cmdstring = @"SELECT * FROM XAccounts";
                cmd = new SqlCommand(cmdstring, sqlConnection);
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                sqlConnection.Close();
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }    
        }

        private void BindData(DataSet ds)
        {
            BindReportGrid(Report, ds.Tables[0]);
        }

        private void BindReportGrid(GridView gridToBind, DataTable dataTableToBind)
        {
            gridToBind.DataSource = dataTableToBind;
            gridToBind.DataBind();
        }

【问题讨论】:

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


    【解决方案1】:

    根据问题 (1),很可能是 ajax 超时。默认超时为 90 秒。增加使用 ScriptManager 的 AsyncPostBackTimeout 属性:

    <asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="400">
    </asp:ScriptManager>
    

    如果 ajax 调用超时,页面上的控件可能无法正常工作,因此增加超时也可以解决问题 (2)。

    【讨论】:

    • 下次遇到这样的问题时我会试试这个。这个项目现在已经完成了。但这似乎是最合理的,因为我确实注意到更新进度在大约 90 秒后消失。所以我会把它标记为答案......干杯!
    【解决方案2】:

    我在使用 ASP.NET UpdateProgress 时也遇到过同样的问题。我通过直接处理脚本管理器事件来修复它:

    <script language="javascript" type="text/javascript">
    
    //adding event handlers for ajax initialize request and end request
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(ShowHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(HideHandler);
    
    function ShowHandler(sender, args) {
        //show div with animation
        pcProcessing_ClientInstance.Show();
    }
    function HideHandler(sender, args) {
        //hide div with animation
        pcProcessing_ClientInstance.Hide();
    }
    
    </script>
    

    【讨论】:

      【解决方案3】:

      也许你想要这个:http://www.codeproject.com/kb/Ajax/ModalUpdateProgress.aspx

      它对我来说效果很好,即使是冗长的操作。

      【讨论】:

        【解决方案4】:

        似乎您的网格在更新面板之外,如果您使用的是更新面板并且您的按钮在更新面板内并且网格在更新面板之外,那么在这种情况下一切都会在服务器端发生,但您不会找到任何用户界面的变化。

        尝试将您的网格放置在更新面板中。

        【讨论】:

        • 不.. 仍然没有出现。我在 UpdatePanel 之外有我的按钮,我正在使用 Asyncpostback 触发器
        【解决方案5】:

        将 UpdateProgress 移到 UpdatePanel 之外。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多