9

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.

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.

<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. enter image description here

2 Answers 2

15

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.

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.

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You. I am aware of this method, BUT was looking for other alternatives as mentioned. The procedure in question would auto turn the fields into input modes on edit click with out any further code.But seems like this is the only way. Is it so? By the way I used insertvisible = true & false in <asp:TemplateField HeaderText="ID" InsertVisible="true">, but it doesnt make any difference. why?
Yes, this is the only way possible for now.Natively the GridView doesn't support the insertion of records the way it supports Edit/Update. So its not possible to auto turn the fields into input modes because GridView will never render into INSERT MODE. Also, The InsertVisible property is not supported for GridView, again because GridView doesn't support InsertMode. I mistaken it for detailsView. Check these 2 link for more:geekswithblogs.net/casualjim/articles/51360.aspx , MSDN: msdn.microsoft.com/en-us/library/…
Updated my answer for InsertVisible property. Just remove the <FooterTemplate> for ID column if ID is a primary key OR an autogenerated field in your database
1

You can't place a commandfield inside TemplateField. But can do like 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> 

2 Comments

The row does not turn into edit mode (textboxes) on edit click, with this method. Thank you though.
You have to set gridview1.EditIndex = e.NewEditIndex;and bind your data again like this gridview1.DataBind(); inside your rowupdated event: OnRowUpdated="gridview1_RowUpdated". After setting the edit index and after rebinding your data, your gridview will render the EditItemTemplate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.