【问题标题】:GridView not data binding on postback of DetailsViewGridView 不是 DetailsView 回发时的数据绑定
【发布时间】:2016-10-10 15:34:43
【问题描述】:

我有同样的问题:

GridView contents don’t update when underlying data changes

但提供的答案对我不起作用,我想请教,因为到目前为止,我已经浪费了 3 天时间试图让 GridView 在 DetailsView 的回发时刷新。

情况是这样的:-

I have a GridView that when a row is selected a DetailsView displays the detailed info.在。

点击编辑时,DetailsView 进入“编辑”模式。

我编辑它并点击更新按钮。

Update 会触发一个事件并正确更新我的 SQL 数据库表。

问题是尽管有 100 篇帖子说使用 GridView1.Databind();在我单击“取消”按钮之前它不会刷新。

我知道它的回发,因为我已经对其进行了调试并在 Page_load(...) 回发中看到了它。

我已经添加了

SqlDataSource1.DataBind();

GridView1.DataBind()

到以下地方,似乎都没有刷新我的 GridView。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ...
    }
    else
    {
    SqlDataSource1.DataBind();
    GridView1.DataBind();
    }
}

还有

protected void DetailsView1_ItemUpdating(object sender,   DetailsViewUpdateEventArgs e)
{
   GridView1.DataBind();
}

还有

protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
    //GridView.SelectedIndex = -1;
   SqlDataSource1.EnableCaching = false;
    // UpdatePanel14.Update();
    SqlDataSource1.DataBind();
    GridView1.DataBind();
    SqlDataSource1.EnableCaching = true;
    // EndEditingGridView();
}

如您所见,我已经在 Page_Load() 下尝试了回发,也包括在 DetailsView 的更新和更新中。我也尝试在 GridView 上设置“ViewState = Disabled”。除非我单击“取消”按钮,否则似乎没有任何东西可以更新其内容。谢谢。

【问题讨论】:

  • 当您点击取消时,会出现回发。我会尝试删除: if (!IsPostBack) { ... } else { } 我会离开 SqlDataSource1.DataBind(); GridView1.DataBind();让我知道这是否是一个解决方案
  • 如何将数据填充到 DetailsView 并更新它?请出示该代码
  • 感谢您的 cmets,将其添加到 Page_Load 的根目录,刚刚停止了 gridview 的选择。我在下面添加了我的答案
  • @naveen,在这里添加代码有点长,因为我有一个单独的用于 DetailsView 的 SQLDatasource,OnSelecting 它会触发一个事件以获取 SelectCommand="SELECT 的选择命令的 IDKey 值* 来自 [DriversID] WHERE DriverIDKey = @DriverID"。我还有一个 Control 参数,它链接到 GridiView 以及一些事件背后的代码。如果您对自己感兴趣,我可以分享它,但如果它只是为了帮助找到答案,我已经准备好了。谢谢。

标签: c# asp.net gridview detailsview ispostback


【解决方案1】:

我从之前所做的一些工作中找到了答案,我发现的工作是删除 Gridviews 数据源,重置其索引并对其进行数据绑定。然后更新UpdatePanel,然后重新连接gridview并再次databind。

我在 DetailsView1_ItemUpdated() 事件结束时调用的函数:

private void EndEditingDetailsView1()
{
    GridView.DataSourceID = null;
    GridView.EditIndex = -1;
    GridView.DataBind();

    DetailsUpdatePanel.DataBind();
    DetailsUpdatePanel.Update(); 

    GridView.DataSourceID = "SqlDataSource";
    GridView.EditIndex = -1;
    GridView.DataBind();
}

工作愉快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多