如何在gridview中添加页脚

时间:2022-04-13 07:51:31

I have a grid with 3 columns - Edit, ID, Movie. I would like to add a footer with an Insert link button, 2 textboxes respectively, but unable to do so. Is it possible.

我有一个有3列的网格 - 编辑,ID,电影。我想添加一个带有插入链接按钮的页脚,分别是2个文本框,但无法这样做。可能吗。

ASPX:

 <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"     
 OnRowEditing="gridview1_RowEditing" 
 OnRowCancelingEdit="gridview1_RowCancelingEdit"
    ShowFooter="true" >
    <Columns>
     <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
      <asp:BoundField DataField="id" HeaderText="ID" />
      <asp:BoundField DataField="movie" HeaderText="MOVIE" />
     </Columns>
</asp:GridView>

When I try the following, there is an error for commandfield which says, this element is not supported.

当我尝试以下操作时,commandfield有一个错误,即不支持此元素。

<Columns>
 <asp:TemplateField>
  <ItemTemplate>
    <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
  </ItemTemplate>
 <FooterTemplate>
    <asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</columns>

The other way would be to use itemTemplate & EditTemplate for each column control. But I find this simple and would like to proceed this way. So Can I add a footer to this structure. 如何在gridview中添加页脚

另一种方法是对每个列控件使用itemTemplate和EditTemplate。但我发现这很简单,并希望以这种方式进行。那么我可以在这个结构中添加一个页脚。

2 个解决方案

#1


11  

YES, It is possible. But this will require using the <FooterTemplate> inside <TemplateField>. Use TemplateFields for each of the columns and also set the FooterTemplate for each of the columns.

对的,这是可能的。但这需要在 中使用 。对每个列使用TemplateFields,并为每个列设置FooterTemplate。

NOTE: The ID column seems here to be a Primary Key. So remove the <FooterTemplate> from the corresponding <TemplateField> defined for ID column, if ID is a Primary Key OR an autogenerated field in your database.

注意:ID列似乎是主键。因此,如果ID是主键或数据库中的自动生成字段,则从为ID列定义的相应 中删除

NOTE II: The <FooterTemplate> simply will contain a TextBox only.

注意II: 只包含一个TextBox。

<Columns>
<asp:TemplateField>
    <EditItemTemplate>
          <asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
                         CommandName="Update" Text="Update"></asp:LinkButton>
                         &nbsp;<asp:LinkButton ID="lnkBtnCancel" runat="server"
                         CausesValidation="False"
                         CommandName="Cancel" Text="Cancel">
          </asp:LinkButton>
    </EditItemTemplate>
    <FooterTemplate>
          <asp:LinkButton ID="lnkBtnInsert" runat="server"  
               CommandName="Insert">Insert</asp:LinkButton>
    </FooterTemplate>
    <ItemTemplate>
          <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
                          CommandName="Edit" Text="Edit"></asp:LinkButton>
                          &nbsp;<asp:LinkButton ID="lnkBtnDelete" runat="server"
                          CausesValidation="False"
                          CommandName="Delete" Text="Delete">
          </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="ID">
    <EditItemTemplate>
          <asp:TextBox ID="TextBoxID" runat="server" Text='<%# Bind("ID") %>'>
          </asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'>
          </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
      <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOVIE">
    <EditItemTemplate>
          <asp:TextBox ID="TextBoxMovie" runat="server" Text='<%# Bind("Movie") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
          <asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
    </FooterTemplate>
    <ItemTemplate>
          <asp:Label ID="Label2" runat="server" Text='<%# Bind("Movie")%>'>
          </asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Now there are 2 ways to insert Data. Either you can use GridView OnRowCommand event or you can handle the OnClick event of your Insert button.

现在有两种方法可以插入数据。您可以使用GridView OnRowCommand事件,也可以处理Insert按钮的OnClick事件。

#2


0  

You can't place a commandfield inside TemplateField. But can do like this:

您不能在TemplateField中放置命令字段。但可以这样做:

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"     
 OnRowEditing="gridview1_RowEditing" 
 OnRowCancelingEdit="gridview1_RowCancelingEdit"
    ShowFooter="true" >
        <Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <!--To fire the OnRowEditing event.-->
            <asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit" 
                Text="Edit">
            </asp:LinkButton>
            <!--To fire the OnRowDeleting event.-->
            <asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete" 
                Text="Delete">
            </asp:LinkButton>
        </ItemTemplate>
        <FooterTemplate>
            <asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
        </FooterTemplate>
    </asp:TemplateField> 
        <asp:BoundField DataField="id" HeaderText="ID" />
      <asp:BoundField DataField="movie" HeaderText="MOVIE" />
</Columns>    
</asp:GridView>

#1


11  

YES, It is possible. But this will require using the <FooterTemplate> inside <TemplateField>. Use TemplateFields for each of the columns and also set the FooterTemplate for each of the columns.

对的,这是可能的。但这需要在 中使用 。对每个列使用TemplateFields,并为每个列设置FooterTemplate。

NOTE: The ID column seems here to be a Primary Key. So remove the <FooterTemplate> from the corresponding <TemplateField> defined for ID column, if ID is a Primary Key OR an autogenerated field in your database.

注意:ID列似乎是主键。因此,如果ID是主键或数据库中的自动生成字段,则从为ID列定义的相应 中删除

NOTE II: The <FooterTemplate> simply will contain a TextBox only.

注意II: 只包含一个TextBox。

<Columns>
<asp:TemplateField>
    <EditItemTemplate>
          <asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
                         CommandName="Update" Text="Update"></asp:LinkButton>
                         &nbsp;<asp:LinkButton ID="lnkBtnCancel" runat="server"
                         CausesValidation="False"
                         CommandName="Cancel" Text="Cancel">
          </asp:LinkButton>
    </EditItemTemplate>
    <FooterTemplate>
          <asp:LinkButton ID="lnkBtnInsert" runat="server"  
               CommandName="Insert">Insert</asp:LinkButton>
    </FooterTemplate>
    <ItemTemplate>
          <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
                          CommandName="Edit" Text="Edit"></asp:LinkButton>
                          &nbsp;<asp:LinkButton ID="lnkBtnDelete" runat="server"
                          CausesValidation="False"
                          CommandName="Delete" Text="Delete">
          </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="ID">
    <EditItemTemplate>
          <asp:TextBox ID="TextBoxID" runat="server" Text='<%# Bind("ID") %>'>
          </asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'>
          </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
      <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOVIE">
    <EditItemTemplate>
          <asp:TextBox ID="TextBoxMovie" runat="server" Text='<%# Bind("Movie") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
          <asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
    </FooterTemplate>
    <ItemTemplate>
          <asp:Label ID="Label2" runat="server" Text='<%# Bind("Movie")%>'>
          </asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Now there are 2 ways to insert Data. Either you can use GridView OnRowCommand event or you can handle the OnClick event of your Insert button.

现在有两种方法可以插入数据。您可以使用GridView OnRowCommand事件,也可以处理Insert按钮的OnClick事件。

#2


0  

You can't place a commandfield inside TemplateField. But can do like this:

您不能在TemplateField中放置命令字段。但可以这样做:

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"     
 OnRowEditing="gridview1_RowEditing" 
 OnRowCancelingEdit="gridview1_RowCancelingEdit"
    ShowFooter="true" >
        <Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <!--To fire the OnRowEditing event.-->
            <asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit" 
                Text="Edit">
            </asp:LinkButton>
            <!--To fire the OnRowDeleting event.-->
            <asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete" 
                Text="Delete">
            </asp:LinkButton>
        </ItemTemplate>
        <FooterTemplate>
            <asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
        </FooterTemplate>
    </asp:TemplateField> 
        <asp:BoundField DataField="id" HeaderText="ID" />
      <asp:BoundField DataField="movie" HeaderText="MOVIE" />
</Columns>    
</asp:GridView>