如何在使用BoundFields的GridView中添加FooterTemplate

3
希望你周末愉快。 我最终找到了一些支持LINQ的编辑/删除事件器。 我知道添加记录事件正在工作,但在尝试一部分后,我不知道如何在页脚添加一些文本框。 所以它是从标题到页脚的一行,没有向右或向左移动。 有人能帮我吗!? 我的代码是:
    <asp:GridView ID="gdview"  runat="server" AutoGenerateColumns="False" DataKeyNames="test_id" OnRowCancelingEdit="gdview_RowCancelingEdit" OnRowDeleting="gdview_RowDeleting" OnRowEditing="gdview_RowEditing" OnRowUpdating="gdview_RowUpdating"
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" ShowFooter="true">
        <Columns>
        <asp:BoundField HeaderText="Test CAT" DataField="test_cat">
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Test INFO" DataField="test_info">
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>  
        <asp:BoundField HeaderText="Test NUMBER" DataField="test_number">
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>   
        <asp:BoundField HeaderText="Test DATE" DataField="test_datetime">
            <ItemStyle Height="20px" Width="150px" />
        </asp:BoundField>       
        <asp:CommandField ShowEditButton="True">
            <ItemStyle Width="100px" />
        </asp:CommandField>
        <asp:TemplateField>
        <ItemTemplate>
          <asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete"   OnClientClick="return confirm('Do you want to delete?')"></asp:LinkButton>      
        </ItemTemplate>
        <ItemStyle Width="100px" />
        </asp:TemplateField>

    </Columns>
    <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
    <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>

好的,但是如果我使用模板,它该如何工作呢?它怎么知道从哪个单元格获取值!?

 Protected Sub gdview_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            'getting table
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            'getting an exiting row
            Dim objtest As testtable = tbltest.SingleOrDefault(Function(p) p.test_id = Integer.Parse(gdview.DataKeys(e.RowIndex).Value.ToString()))
            If objtest IsNot Nothing Then
                'modifying the row
                objtest.test_cat = DirectCast(gdview.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text
                objtest.test_info = DirectCast(gdview.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text
                objtest.test_number = DirectCast(gdview.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
                objtest.test_datetime = DirectCast(gdview.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
                'saving rows added
                db.SubmitChanges()
                gdview.EditIndex = -1
                bindgrid()
                ' Me.lblMsg.Text = "Updated Successfully"
            Else
                'Me.lblMsg.Text = "Employee not found"
            End If
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using

+1 因为你早些时候提出了问题,所以今天我受益了。 - bonCodigo
1个回答

5
您可以尝试使用

标签。

<columns>
  <asp:templatefield headertext="Test" >
    <itemtemplate>
       .... 
    </itemtemplate>
    <footertemplate>
      <asp:TextBox id="TestTbx"   runat="server"/>
    </footertemplate>
  </asp:templatefield>
</columns>

链接: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx

这个链接是关于 TemplateField.FooterTemplate 属性的。该属性允许您指定一个控件模板,该模板将在数据绑定控件的页脚中显示。页脚通常用于显示汇总信息或其他与整个数据集相关的信息。使用此属性可以更好地控制页脚的外观和行为。

@Thomas 将您的 BoundField 替换为 TemplateField,并添加页脚。BoundField 是基本字段,您无法自定义它。如果您希望进行自定义,则必须开发另一个 BoundField 并覆盖其基本行为。 - Aghilas Yakoub
嗨,Aghilas,请看一下我的添加...Code Behind。 - Thomas BP
你可以使用 e.Item.FindControl("IdOfControlInRow") 修改你的代码。 - Aghilas Yakoub
那么我不需要添加edittemplates来使编辑工作,是吗? - Thomas BP
如果您计划编辑此字段(现在是“BoundField”),则需要使用“EditTemplate”。通常情况下,被定义为“BoundField”的列似乎不会在运行时进行编辑。我想您的情况也是如此。您只想插入而不是编辑。因此,正如@AghilasYakoub所演示的那样,它只是一个“templatefield”,以支持使用“FooterTemplate”进行“insert”的功能。 - bonCodigo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接