2013-05-07 89 views
0

通常,當我想訪問Sitecore中的當前項目時,我會通過Sitecore.Context.Item。在創建最終用戶使用的工具時,此功能會很好,但不適用於Sitecore管理員使用的工具。如果我想在Content Editor本身中顯示某個自定義字段,則Context.Item是對Content Editor的引用,而不是在編輯器中選擇的節點。如何從Sitecore內容編輯器的自定義字段訪問父項目?

大多數情況下,我可以通過使用ItemID屬性來解決此問題,但是如果我在該字段上有事件調度程序,則這些將不再有權訪問ItemID。例如:

protected override void OnPreRender(EventArgs e) 
{ 
    if (IsEvent) 
    { 
     // ItemID is defined here! 
     Button live = FindControl(GetID(LiveButton)) as Button; 
     if (live != null) 
     { 
      live.ServerProperties["Click"] = string.Format("{0}.LiveClicked", ID); 
     } 
    } 
} 

public void LiveClicked() 
{ 
    // ItemID is blank here! 
    DoSomething(); 
} 

如何獲取項目ID在我的監聽器(像LiveClicked以上)?

+0

我意識到這不是自定義字段實際應該做的事情,但客戶已明確聲明他希望在內容編輯器的主體中具有某些功能。 – cwallenpoole 2013-05-07 18:32:36

回答

0

我解決這個問題的方法是通過一個叫做ServerProperties的東西,我在每個聆聽者中稱這個函數等價。

private void SyncID() 
{ 
    var live = FindControl(GetID(LiveButton)) as Button; 
    if (live != null) 
    { 
     if(string.IsNullOrEmpty(ItemID)) 
     { 
      ItemID = live.ServerProperties["owner_id"].ToString(); 
     } 
     live.ServerProperties["owner_id"] = ItemID; 
    } 
} 
+0

我意識到這是代碼味道。這讓我感到十分錯誤,但它解決了這個問題。如果有更好的答案,我會很樂意接受並取代它。 – cwallenpoole 2013-05-07 18:31:31

相關問題