2013-07-22 73 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()中,點擊'取消'按鈕後,所有三個事件都按順序觸發。