[英]gridview on page won't refresh, even when calling databind again
我所做的所有研究似乎都表明,如果我再次简单地调用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.";
}
}
}
}
我建议您将网格放入“更新面板”中。 看起来当您单击按钮时,回发页面不会刷新,因此网格也不会...
我的头撞到桌子上后,我终于以回旋的方式偶然发现了答案。
我的SQL_IsJobRunning和SQL_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.";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.