1

我有一个从 LinqDataSource 填充的 GridView。当我更新一行时,RowCommand 会触发并将更改保存到数据库中,但 Grid 不会刷新。我将它放在 UpdatePanel 中,并在 RowCommand 处理程序中显式调用 Update(),但没有回发,页面只是在编辑模式下坐在那里。单击取消后,它将返回到仅查看状态,并且网格会显示新值。

我的怀疑是关于数据源的 GridView 接线中的某些内容是错误的。不过,也不例外。标记的精简副本如下。有任何想法吗?

<asp:UpdatePanel ID="uPanel" runat="server" UpdateMode="Conditional" 
  EnableViewState="true" ChildrenAsTriggers="true">
  <ContentTemplate>
    <asp:LinqDataSource ID="YieldDataSource" runat="server" 
        ContextTypeName="myhDataContext" TableName="vw_drug_yields" 
        OnSelecting="YieldDataSource_Selecting" EnableUpdate="true" />
    <asp:GridView ID="YieldGridView" runat="server" Width="900px" 
         OnRowDataBound="editGrid_RowDataBound"
         DataSourceID="YieldDataSource" EnableViewState="true"
         OnRowCommand="YieldGridView_RowCommand">
    <Columns>
      <asp:TemplateField HeaderText="Net Fill" ItemStyle-HorizontalAlign="Center">
          <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "net_fill") %>
          </ItemTemplate>
          <EditItemTemplate><asp:TextBox ID="tbNetFill" runat="server" 
          Text='<%# DataBinder.Eval(Container.DataItem, "net_fill") %>' >
          </asp:TextBox></EditItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField ShowHeader="False" ItemStyle-Width="40px">
          <ItemTemplate>
             <asp:ImageButton CommandName="Edit" ID="btnEdit" SkinID="btnEdit" 
             runat="server" ToolTip="Edit" CausesValidation="false"/> 
          </ItemTemplate>
          <EditItemTemplate>
             <asp:ImageButton CommandName="Update" ID="btnSubmit" SkinID="btnSubmit" 
             runat="server" ToolTip="Save" CausesValidation="true" 
             CommandArgument="<%# ((GridViewRow) Container).DataItemIndex %>" /> 
             <asp:ImageButton CommandName="Cancel" ID="btnCancel" SkinID="btnCancel" 
             runat="server" ToolTip="Cancel" CausesValidation="false"/> 
          </EditItemTemplate>
      </asp:TemplateField>
    </Columns>
   </asp:GridView></ContentTemplate></asp:UpdatePanel>

处理程序:

protected void YieldGridView_RowCommand(Object sender, 
      GridViewCommandEventArgs e) {
  if (e.CommandName == "Update") {
      try {
          int index = Convert.ToInt32(e.CommandArgument);

          GridViewRow gdrow = YieldGridView.Rows[index];

          // do some validation and handle update

             db.SubmitChanges();

          YieldGridView.DataBind();
          uPanel.Update();
      }
      catch (Exception ex) {
          ShowError(this, "Error while updating yields", ex, true);
      }
  }
4

3 回答 3

3

删除 UpdatePanel 并关闭 GridView 的回调后,单击更新按钮时收到以下错误:

在 ViewState 中存储的原始值中找不到与给定键匹配的行。确保 'keys' 字典包含与前一个 Select 操作返回的行相对应的唯一键值。

问题是这些数据没有单一的唯一键,因为它是从不同来源动态组装的。它不应该是必需的,但似乎带有 LinqDataSource 的 GridView 没有它就无法运行。此外,我正在从没有主键的视图中填充网格。该解决方案涉及 3 个更改:

  • 禁用 LinqDataSource 的更新
  • 将命令名称从 Update 更改为 MyUpdate(这样 Linq 不会尝试自动连接它)
  • 在调用 UpdatePanel 上的 Update 之前设置 YieldGridView.EditIndex = -1

谢谢你的帮助。

于 2010-01-19T22:12:41.593 回答
0

在调用 Update() 之前,调用 GridView.DataBind() 以重新绑定 LINQDataSource 控件。

于 2010-01-19T18:03:10.857 回答
0

试试这个:

YieldGridView.DataBind();
//though this seems useless after call to DataBind, but lets just try :)
YieldGridView.EditIndex = -1;
uPanel.Update();
于 2010-01-19T18:19:14.827 回答