[英]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.