2013-05-01 34 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