Saravanan Sachi
Saravanan Sachi

Reputation: 2612

CommandName = Insert in EditTemplate of ASP.NET ListView throws "Insert can only be called on an insert item"

I am supporting a web application. In that, there are two tables - TaxCode and TaxRate. TaxCode has 1 to many relationship with TaxRate.

The UI has a ListView with LayoutTemplate, ItemTemplate and EditTemplate to show TaxCode. When the users selects a tax code in EditTemplate it shows a CutomGridView that allows the user to create or edit tax rates for that particular tax code. This CustomGridView has 3 rows each has 4 template fields as shown below.

<asp:TemplateField HeaderStyle-CssClass="highlightTitlebar" HeaderStyle-Width="5%" HeaderStyle-Height="30px">
<HeaderTemplate>
  <custom:CustomImageButton ID="imgAdd" runat="server" ImageUrl="image/add_round.gif" OnClick="imgAddTaxRateDetail_Click" CausesValidation="False"/>
</HeaderTemplate>
<ItemTemplate>
   <custom:CustomImageButton ID="imgEdit" runat="server" ImageUrl="image/edit.gif"  CommandName="Edit" />
</ItemTemplate>          
<EditItemTemplate>
   <asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="image/update.gif" CommandName="Update" />
   <asp:HiddenField runat="server" ID="hfId" Value='<%# Bind("Id") %>' />
</EditItemTemplate>
<FooterTemplate>
   <asp:ImageButton ID="imgInsert" runat="server" ImageUrl="image/insert.gif" CommandName="Insert" OnClick="imgInsert_Click" />
</FooterTemplate>

Each row in the CustomGridView is a template field. In, below image EffectiveOn section is CustomGridView,

How the CustomGridView is binded with ListView

When I try to save TaxRate with proper EffectiveOn and Rate, it throws "Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert." error as the ListView doesn't have InsertTemplate. But the record gets inserted into the DB.

Please let me know if there is any way to resolve this issue?

Upvotes: 1

Views: 1057

Answers (1)

codeandcloud
codeandcloud

Reputation: 55210

The error is self explanatory.
Take a look at this: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx

So you can do either of these things.

  1. Create an InsertItemplate and insert using the ItemInserted event of the listview
  2. Change the CommandName to CommandName="InsertData" and catch that event on the ItemCommand

Upvotes: 1

Related Questions