【问题标题】:Making a GridView footer data bound like a Row data使 GridView 页脚数据像行数据一样绑定
【发布时间】:2013-02-13 21:38:49
【问题描述】:

我有一个使用 GridViews 的 C# 创建的 ASP.NET 项目。页脚行包含基于列数据的总数与来自数据的设定值的比率,说明应该有多少。用户需要能够修改第二个值并更新页脚。我无法找出如何做到这一点,或者即使有可能。我考虑在下面使用另一个 GridView,但确保两者之间的列线同步是一场噩梦。有什么想法吗?

另外,当我更改列数据(使用编辑/更新行)时,当我单击更新时,总数不会更新,但当我再次单击编辑时会更新。谁能告诉我这是为什么以及如何在更新时更新页脚中的总数?

【问题讨论】:

  • 你必须给我们看一些代码!
  • 能否发布您的 GridView 客户端代码和 OnEdit 和 OnUpdate 事件的服务器端代码?

标签: c# asp.net data-binding gridview footer


【解决方案1】:

如果您发现您编辑的字段“一键后”刷新,通常是因为您认为您已反弹您的 GridView 但实际上没有或您在进行更新之前已反弹。

使用 Gridview,每当您在页脚中放置字段时,一个典型的场景是在页面生命周期的数据绑定操作期间计算它的值

就像这样,您的 .aspx 文件定义了一些页脚字段,通常是一些已转换为 TemplateFields 的 BoundField。这是一个sn-p:

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" ...>
  <Columns>
    <asp:BoundField DataField="Field1" HeaderText="Title" SortExpression="Field1" />
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Field2") %>' ></asp:Label>
      </ItemTemplate>
      <FooterTemplate>
        <asp:Label ID="FooterLabel1" runat="server" Text="" ></asp:Label>
      </FooterTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

背后代码中的 GridView 事件:这通常是您根据更改的行值填充页脚字段所需执行的操作。这是 VB,但您应该能够很容易地转换它。

// Create a variable at the Page level that will exist for 
// the duration of the Page LifeCycle
Private FooterLabel1SubTotal as Double

// Initialize
Private Sub GridView1_DataBinding(sender As Object, e As EventArgs) Handles GridView1.DataBinding
  FooterLabel1SubTotal = 0.0
End Sub

// Accumulate
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
  If e.Row.RowType = DataRow Then
    Dim Field2 as Label = e.Row.FindControl("Field2")
    FooterLabel1SubTotal += Convert.ToDouble(Field2.Text)
  End If
End Sub

// Populate Footer with formated value
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
  FooterLabel1 = GridView1.FooterRow.FindControl("FooterLabel1")
  FooterLabel1.Text = String.Format("{0:F2}", FooterLabel1SubTotal)

End Sub

现在,如果您使用 GridView 的任何内置编辑功能,这将导致回发并导致 GridView 重新绑定,这应该每次都重新计算页脚字段。

【讨论】:

    猜你喜欢
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多