【问题标题】:GridView on page won't refresh, even when calling DataBind again即使再次调用 DataBind,页面上的 GridView 也不会刷新
【发布时间】:2012-03-01 06:50:53
【问题描述】:

我所做的所有研究似乎都表明,如果我再次调用DataBind(),那么我的GridView 将会得到更新。如果我正在调试并单步执行我的代码,这似乎只是这种情况,GridView 刷新得很好。但是,如果我在调试模式下运行应用程序时不单步执行我的代码,则下面的btnFileImport_Click 方法不会刷新我的GridView。这可能与我通过使用 SSIS 包加载文件来更新GridView 使用的数据这一事实有关吗?下面是后面的代码:

namespace InternationalWires
{
    public partial class Default_Corporate : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection
        (
            ConfigurationManager.ConnectionStrings
            ["InternationalWiresConnection"].ToString()
        );
        SqlCommand cmd = null;
        SqlServerAgent sqlAgent = new SqlServerAgent();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRatesGrid();
            }
        }
        public void BindRatesGrid()
        {
            conn = new SqlConnection
            (
                ConfigurationManager.ConnectionStrings
                ["InternationalWiresConnection"].ToString()
            );
            SqlDataAdapter da = new SqlDataAdapter("spGetRates", conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            da.Fill(ds);
            grdRates.DataSource = ds.Tables[0].DefaultView;
            grdRates.DataBind();
        }
        protected void btnFileImport_Click(object sender, EventArgs e)
        {
            // Get the filename and path from the user.
            // Must be in UNC format or SSIS will fail.
            string filename =
                Path.GetFullPath(fileSelect.PostedFile.FileName);
            // Update the settings table to the value from above.
            try
            {
                conn = new SqlConnection
                (
                    ConfigurationManager.ConnectionStrings
                    ["InternationalWiresConnection"].ToString()
                );
                cmd = new SqlCommand
                (
                    "UPDATE Settings SET settingValue = '" + filename +
                    "' WHERE settingName = 'SSISRatesImportFile'", conn
                );
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                // To do: handle exceptions.
            }
            finally 
            {
                if (conn != null) conn.Dispose();
                if (cmd != null) cmd.Dispose();
            }
            // Set the name of the SSIS package to run.
            sqlAgent.SSISName = ConfigurationManager
                .AppSettings["ratesImportPackage"].ToString();
            // Start the job.
            sqlAgent.SQL_SSISPackage();
            // Do nothing while waiting for job to finish.
            while (sqlAgent.SQL_IsJobRunning()) { }
            if (sqlAgent.SQL_JobSucceeded())
            { 
                lblStatus.Text = "Import Succeeded";
                BindRatesGrid();
            }
            else
            { 
                lblStatus.Text =
                "Import Failed. " + 
                "Please contact IT for failure details on SSIS import package."; 
            }

        }
    }
}

【问题讨论】:

  • 我认为您的 // TO DO: handle exceptions 是 GridView 未更新的原因 ;-)
  • 当我单步执行代码时,它从未遇到异常,所以我不确定为什么会这样。
  • 在该部分添加一些代码没有任何区别。

标签: c# asp.net gridview data-binding


【解决方案1】:

我建议将您的网格放入更新面板。看起来当您单击按钮时,页面没有在回发中刷新,因此网格不是......

【讨论】:

  • 似乎是个好主意,但显然 FileUpload 不能很好地与 UpdatePanel 配合使用。我运气不太好。
【解决方案2】:

我的头在桌子上敲了很多次之后,我终于在一个迂回的路上偶然发现了答案。

我的SQL_IsJobRunningSQL_JobSucceeded 方法在SQL 中使用sp_help_job 来确定作业是否仍在运行,以及它是否成功。我正在处理要在我的标签中显示的成功/错误消息,当我得到错误消息时,我意识到“current_execution_status”正在显示工作已完成,但“last_run_outcome”在我的时候尚未更新代码正在查看该值。所以我在这两种方法之间暂停(Thread.Sleep(4000)),让数据库有机会在我检查之前记录last_run_outcome

这解决了我的标签错误消息问题,并且还解决了我的GridView 问题。随着暂停到位,GridView 也成功更新。 GridView 必须有一些事情发生得太快而无法正确更新。我只是希望我知道什么。可能BindRatesGrid() 方法在数据提交到数据库之前正在运行。

// Do nothing while waiting for job to finish.
while (sqlAgent.SQL_IsJobRunning()) {}
Thread.Sleep(4000);
if (sqlAgent.SQL_JobSucceeded())
{ 
    lblStatus.Text = "Import Succeeded";
    BindRatesGrid();
}
else
{ 
    lblStatus.Text = "Import Failed. " +
    "Please contact IT for failure details on SSIS import package."; 
}

【讨论】:

    猜你喜欢
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多