c# - 页面上的 GridView 不会刷新,即使再次调用 DataBind

标签 c# asp.net gridview data-binding

我所做的所有研究似乎表明,如果我再次调用 DataBind(),我的 GridView 就会更新。这似乎只有在我调试和单步执行我的代码时才会出现这种情况,GridView 刷新良好。但是,如果我在 Debug模式下运行应用程序时没有单步执行我的代码,下面的 btnFileImport_Click 方法不会刷新我的 GridView。这与我更新 GridView 通过使用 SSIS 包加载文件所使用的数据有什么关系吗?下面是背后的代码:

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

        }
    }
}

最佳答案

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

关于c# - 页面上的 GridView 不会刷新,即使再次调用 DataBind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9200048/

相关文章:

c# - 在 Visual Studio C# 中,有没有办法将 Web Config 转换应用于 Javascript 文件?

c# - 在后面的代码中从 DataTable 设置 javascript 数组

c# - ViewBag.Title 在我的代码 ASP.NET MVC 4 中不起作用

c# - 访问 gridview 隐藏域

c# - 在 sql for asp.net gridview 中使用 ROW_NUMBER() 自定义分页

c# - 无泄漏中断循环

c# - 如何从 MemberAccessExpressionSyntax 获取方法主体?

c# - e.Row.findcontrol() 在数据绑定(bind)后返回 null 但所有数据绑定(bind)字段都正常工作

c# - asp.net 未找到 OpenCV 非托管 DLL

android - 通用图像加载器 gridview 在 notifyDataSetChanged 调用后闪烁