How to add footer in gridview

I have a grid with three columns - Edit, ID, Movie. I would like to add a footer with the "Insert Link" button, 2 text fields respectively, but could not do this. Is it possible.

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 to do the following, there is an error for the command field that says this item is not supported.

 <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> 

Another way would be to use itemTemplate and EditTemplate for each column control. But I find it simple and would like to continue this path. Can I add a footer to this structure. enter image description here

+6
source share
2 answers

YES, Maybe. But for this, you will need to use <FooterTemplate> inside <TemplateField> . Use TemplateFields for each of the columns, and set the FooterTemplate for each of the columns.

NOTE. The ID column appears here as the primary key. Therefore, remove the <FooterTemplate> from the corresponding <TemplateField> defined for the ID column if the ID is a Primary Key OR an automatically generated field in your database.

NOTE II: <FooterTemplate> will simply contain only a text field.

 <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 two ways to insert data. Either you can use the GridView OnRowCommand event, or you can handle the OnClick event of your Insert button.

+10
source

You cannot put a command field inside a TemplateField. But you can do this:

 <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> 
0
source

Source: https://habr.com/ru/post/953871/


All Articles