2013-07-22 72 views
1

我有查看和编辑模式的一个FormView,在一个UpdatePanel:取消FormView的编辑模式?

<asp:UpdatePanel runat="server"> 
    <ContentTemplate> 
     <asp:FormView runat="Server" DefaultMode="ReadOnly" DataSourceID="_myDataSource" > 
      <ItemTemplate> 

       <!-- View controls omitted --> 
       <asp:Button runat="server" CommandName="Edit" Text="Edit" /> 
      </ItemTemplate> 

      <EditItemTemplate> 

       <!-- Edit controls omitted --> 
       <asp:Button runat="server" CommandName="Update" Text="Save" /> 
       <asp:Button runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" /> 
      </EditItemTemplate> 
     </asp:FormView> 
    </ContentTemplate> 
</asp:UpdatePanel> 

<!-- Data source (may attributes omitted) --> 
<asp:ObjectDataSource ID="_myDataSource" runat="server" /> 

EditUpdate按钮/命令做工精细,但Cancel按钮什么都不做。

任何想法?

+0

单击取消按钮后,FormView将从编辑模式中跳出。那么你想要取消按钮做什么? –

+0

这就是我想要的,但它什么都不做。甚至没有回传。 “编辑”和“更新”按钮只需设置“CommandName”即可按预期工作。 –

回答

2

页面可能看起来不闪烁,但点击取消按钮2事件被FormView引发并回发确实发生。你可以参考MSDN。 (在备注部分表)

一个 '取消' 按钮的功能是:

Cancels an edit or insert operation and returns the FormView control to the mode specified by the DefaultMode property. Raises the ModeChanged and ModeChanging events.

所以,你需要处理ModeChanged和ModeChanging事件,如下图所示:

<asp:formview id="EmployeeFormView" 
     datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" 
     onmodechanged="EmployeeFormView_ModeChanged" 
     onmodechanging="EmployeeFormView_ModeChanging" 
     runat="server"> 

所以,在'取消'按钮点击后你想运行的任何代码都应该按照要求使用这两个事件。

我已将调试器放在Page_Load,modeChanging()& modechanged()中,点击'取消'按钮后,所有三个事件都按顺序触发。