Viktor Pless
Viktor Pless

Reputation: 161

asp FormView delete not firing

I have a formview with an insert and a delete button. The insert button fires the onInserting event, but the delete button does not fire the onDeleting event. Why? Here's my code:

<asp:FormView ID="LanguagesFormView" runat="server" DefaultMode="Insert" DataSourceID="LanguageSqlDataSource" OnItemInserting="LanguagesFormView_ItemInserting" OnItemDeleting="LanguagesFormView_ItemDeleting">
    <InsertItemTemplate>
        <dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DataSourceID="LanguageSqlDataSource" ValueField="LanguageID" TextField="LanguageEN"></dx:ASPxComboBox>
        <dx:ASPxListBox ID="ASPxListBox1" runat="server" DataSourceID="ProjectLanguageSqlDataSource" TextField="LanguageEN"></dx:ASPxListBox>
        <dx:ASPxButton ID="addLangASPxButton" runat="server" CommandName="Insert" Text="Add"></dx:ASPxButton>
        <dx:ASPxButton ID="deleteLangASPxButton" runat="server" CommandName="Delete" Text="Delete"></dx:ASPxButton>
    </InsertItemTemplate>
</asp:FormView>

I tested it by placing breakpoints at the event handlers.

Upvotes: 0

Views: 703

Answers (1)

matk
matk

Reputation: 1518

The OnItemDeleting event doesn't fire while the FormView is in Inserting mode - which makes sense, because how would you know which record to delete if you're just inserting a new one?

If you put the delete button in the ItemTemplate instead of the InsertItemTemplate then the delete event handler will fire correctly.

<asp:FormView ID="LanguagesFormView" runat="server" DataSourceID="LanguageSqlDataSource"
    OnItemInserting="LanguagesFormView_ItemInserting" OnItemDeleting="LanguagesFormView_ItemDeleting" DataKeyNames="LanguageID">
    <ItemTemplate>
        ...
        <dx:ASPxButton ID="deleteLangASPxButton" runat="server" CommandName="Delete" Text="Delete" />
    </ItemTemplate>
</asp:FormView>

Also note that you should set the DataKeyNames property on the FormView in order to get the key you want to delete server-side.

Upvotes: 2

Related Questions