简体   繁体   English

即使再次调用databind,页面上的gridview也不会刷新

[英]gridview on page won't refresh, even when calling databind again

All the research I've done seems to indicate that if I simply call DataBind() again then my gridview will get updated. 我所做的所有研究似乎都表明,如果我再次简单地调用DataBind(),那么gridview将得到更新。 This only seems to be the case if I'm debugging and stepping through my code, the gridview refreshes fine. 仅当我在调试并逐步执行代码时,Gridview才能正常刷新。 However, if I don't step through my code while running the app in debug mode, the btnFileImport_Click method below doesn't refresh my gridview. 但是,如果在调试模式下运行应用程序时未逐步执行代码,则下面的btnFileImport_Click方法不会刷新我的gridview。 Could it have anything to do with the fact that I'm updating the data the gridview uses by loading a file using an SSIS package? 它是否与我通过使用SSIS包加载文件来更新gridview使用的数据有关? Below is the codebehind: 下面是代码:

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."; 
            }

        }
    }
}

I would suggest putting your grid into an Update Panel. 我建议您将网格放入“更新面板”中。 It looks like when you click the button, the page isn't refreshed in the postback thus the grid isn't... 看起来当您单击按钮时,回发页面不会刷新,因此网格也不会...

After much banging of my head on the desk, I finally stumbled across the answer in a roundabout way. 我的头撞到桌子上后,我终于以回旋的方式偶然发现了答案。

My SQL_IsJobRunning and SQL_JobSucceeded methods use sp_help_job in SQL to figure out whether or not the job is still running, and whether or not it succeeded. 我的SQL_IsJobRunning和SQL_JobSucceeded方法在SQL中使用sp_help_job来确定作业是否仍在运行以及是否成功。 I was working on the success/error messages to display in my label and when I was getting the wrong ones I realized that the 'current_execution_status' was showing the job was done, but the 'last_run_outcome' had not yet been updated by the time my code was looking at the value. 我正在处理要在标签中显示的成功/错误消息,当我收到错误消息时,我意识到'current_execution_status'表示作业已完成,但是到我发布时,'last_run_outcome'尚未更新代码正在查看值。 So I put a pause (Thread.Sleep(4000)) in between the two methods to give the database a chance to log the last_run_outcome before I checked it. 因此,我在这两种方法之间添加了一个暂停(Thread.Sleep(4000)),使数据库有机会在检查前记录last_run_outcome。

This solved my label error message issue, and had the pleasant side effect of also solving my gridview problem. 这解决了我的标签错误消息问题,并且还具有解决我的gridview问题的令人愉快的副作用。 With the pause in place the gridview also updates successfully. 暂停到位后,gridview也将成功更新。 There must have been something happening too fast for the gridview to update properly. 某些事情发生得太快了,GridView无法正确更新。 I just wish I knew what. 我只是希望我知道什么。 Perhaps the BindRatesGrid() method was being run before he data was committed in the database. 在将数据提交数据库之前,可能正在运行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."; 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM