2013-05-01 32 views
4

保存Sitecore項目時我試圖顯示一個彈出窗口與用戶交互。根據他們改變的數據,我可能會顯示一系列1或2個彈出窗口,詢問他們是否要繼續。我已經想出瞭如何使用OnItemSaving管道。這很簡單。我無法弄清楚的是如何顯示彈出窗口並對用戶輸入做出反應。現在我想我應該以某種方式使用Sitecore.Context.ClientPage.ClientResponse對象。以下是一些代碼,顯示了我正在嘗試執行的操作:如何在保存Sitecore項目時顯示彈出窗口?

public class MyCustomEventProcessor 
{ 
    public void OnItemSaving(object sender, EventArgs args) 
    { 
     if([field criteria goes here]) 
     { 
     Sitecore.Context.ClientPage.ClientResponse.YesNoCancel("Are you sure you want to continue?", "500", "200"); 
     [Based on results from the YesNoCancel then potentially cancel the Item Save or show them another dialog] 
     } 
    } 
} 

我應該使用其他方法嗎?我看到還有ShowModalDialog和ShowPopUp以及ShowQuestion等等,我似乎無法找到關於這些的任何文檔。此外,我甚至不確定這是否是正確的方式來做這樣的事情。

回答

7

的過程是這樣的(我要指出,我從來沒有試過這種從項目:節省事件,但是,我認爲它應該工作):

  1. item:saving事件,調用客戶端流水線中的對話處理器,並傳遞一組參數。
  2. 處理器執行以下兩件事之一;顯示對話框,或消費響應。
  3. 收到響應後,處理器將使用它並在那裏執行您的操作。

下面是一個說明上述步驟的例子:

private void StartDialog() 
{ 
    // Start the dialog and pass in an item ID as an argument 
    ClientPipelineArgs cpa = new ClientPipelineArgs(); 
    cpa.Parameters.Add("id", Item.ID.ToString()); 

    // Kick off the processor in the client pipeline 
    Context.ClientPage.Start(this, "DialogProcessor", cpa); 
} 

protected void DialogProcessor(ClientPipelineArgs args) 
{ 
    var id = args.Parameters["id"]; 

    if (!args.IsPostBack) 
    { 
     // Show the modal dialog if it is not a post back 
     SheerResponse.YesNoCancel("Are you sure you want to do this?", "300px", "100px"); 

     // Suspend the pipeline to wait for a postback and resume from another processor 
     args.WaitForPostBack(true); 
    } 
    else 
    { 
     // The result of a dialog is handled because a post back has occurred 
     switch (args.Result) 
     { 
      case "yes": 

       var item = Context.ContentDatabase.GetItem(new ID(id)); 
       if (item != null) 
       { 
        // TODO: act on the item 

        // Reload content editor with this item selected... 
        var load = String.Format("item:load(id={0})", item.ID); 
        Context.ClientPage.SendMessage(this, load); 
       } 

       break; 

      case "no": 

       // TODO: cancel ItemSavingEventArgs 

       break; 

      case "cancel": 
       break; 
     } 
    } 
} 
+0

感謝這麼多的工作!現在還有一個問題。如果不是簡單的Yes,No,Cancel,我想顯示一個更復雜的彈出窗口,詢問用戶一系列問題。根據第一個問題的答案,我可以完全取消保存,或者我可以繼續保存,然後運行一些自定義代碼,然後詢問他們第二個問題。根據第二個問題的答案,我將運行一些更多的自定義代碼,然後完全關閉彈出窗口。我會使用SheerResponse.ShowPopup方法嗎? – 2013-05-01 18:16:37