title: ListView cancel editItem
body: <p>I'm having trouble returning to listview and cancelling the edit of items using ASP and C#.</p>
  
  <p>I already have the same answer as <a href="https://stackoverflow.com/questions/16608792/listview-edititemlayout-cancel-action">this</a>
   but I'm still getting the error message of <code>Cancel can only be called from the currently-edited record or an insert item.</code></p>
  
  <p>Here is the offending code - any pointers on what is causing this would be very useful:</p>
  
  <p>I also have a layout, selected template, and insert which are all identical.</p>
  
  <pre><code>&lt;asp:ListView ID="ListView1" runat="server" DataSourceID="sqlDatasource1"
                      EnableViewState="true" DataKeyNames="EndUserId" AllowPaging="True"
                      OnItemCommand="ListView1_ItemCommand" OnItemCanceling="ListView1_ItemCanceling"&gt;
  
      &lt;EditItemTemplate&gt;
          &lt;tr&gt;
              &lt;asp:Label runat="server" ID="tbId" Text='&lt;%#Eval("EndUserId") %&gt;' Visible="false" /&gt;
              &lt;td&gt;
                  &lt;asp:TextBox runat="server" ID="tbEmail" Text='&lt;%#Eval("Email") %&gt;' /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:TextBox runat="server" ID="tbRole" Text='&lt;%#Eval("Role") %&gt;' /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update"&gt;Update&lt;/asp:LinkButton&gt;
                  &lt;asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel"&gt;Cancel&lt;/asp:LinkButton&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/EditItemTemplate&gt;
  </code></pre>
  
  <p>c# backend controlling</p>
  
  <pre><code>    protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
      {
          ListView1.EditIndex = -1;
          ListView1.DataBind();      
      }
  
  protected void Page_Load(object sender, EventArgs e)
          {
              ListView1.EditIndex = -1;
              ListView1.InsertItemPosition = InsertItemPosition.LastItem;
  
              if (!IsPostBack)
              {
  
              }
          }
  </code></pre>
  
  <p><strong>added backend</strong></p>
  
  <pre><code>protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
          {
              if (e.CommandName == "Update")
              {
                  Label tbId = (Label)e.Item.FindControl("tbId");
                  TextBox tbEmail = (TextBox)e.Item.FindControl("tbEmail");
                  TextBox tbPass = (TextBox)e.Item.FindControl("tbPass");
  
                  string updateCommand =
                      "UPDATE [EndUser_tbl] SET [Email]='" +
                      tbEmail.Text + "', [Password]='" + tbPass.Text +
                      "'WHERE EndUserId=" + Convert.ToInt32(tbId.Text) + ";";
                  sqlDatasource1.UpdateCommand = updateCommand;
              }
              if (e.CommandName == "Delete")
              {
                  Label id = (Label)e.Item.FindControl("tbId");
                  string deleteCommand = "DELETE FROM [EndUser_tbl] " +
                          "WHERE EndUserId=" + Convert.ToInt32(id.Text);
                  sqlDatasource1.DeleteCommand = deleteCommand;
              }
              if (e.CommandName == "Insert")
              {
                  TextBox tbEmail = (TextBox)e.Item.FindControl("tbEmail");
                  TextBox tbPass = (TextBox)e.Item.FindControl("tbPass");
  
  
                  string insertCommand = "INSERT INTO [EndUser_tbl] " +
                          "([Email],[Password],[ContractRef],[Role])" +
                          "VALUES ('" + tbEmail.Text + "','" + tbPass.Text + 
                              "','" + tbPass.Text + "');";
  
                  sqlDatasource1.InsertCommand = insertCommand;
              }
          }
  </code></pre>