我有一个带有插入和删除按钮的formview。 insert按钮触发onInserting事件,但delete按钮不会触发onDeleting事件。为什么?这是我的代码:
<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>
我通过在事件处理程序中放置断点来测试它。
答案 0 :(得分:2)
当FormView处于插入模式时,OnItemDeleting事件不会触发 - 这是有道理的,因为如果你只是插入一个新记录,你怎么知道要删除哪个记录?
如果将删除按钮放在ItemTemplate而不是InsertItemTemplate中,则删除事件处理程序将正确触发。
<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>
另请注意,您应在FormView上设置DataKeyNames属性,以获取要在服务器端删除的密钥。