Sitecore: Dynamic Sublayouts Loading




In generalEnties.cs made change in TabsItem class.

Line :05

public class TabsItem

{

public string TabTitle { get; set;

}

public string OptionalText { get; set;

}

public string TabID{ get; set;

}

public string HideTab { get; set;

}

}

in tabs.cs I have added a line to populate TabID in

function; Line :10



public List
GetPartsTabs(Item
currentPageItem)
{
List tabItemList = new
List();
MultilistField
TabsItemCollection = currentPageItem.Fields["Parts Page
Tabs"];
foreach (Item
eachTab in
TabsItemCollection.GetItems())
{
TabsItem oTabsItem =
new TabsItem();
oTabsItem.TabTitle =
eachTab.Fields["Tab Title"].ToString();
oTabsItem.OptionalText =
eachTab.Fields["Optional Text"].ToString();
oTabsItem.TabID =
eachTab.Fields["Tab Title"].ToString().Trim();
CheckboxField chkHideTab
= eachTab.Fields["Hide Tab"];
if
(chkHideTab.Checked)
oTabsItem.HideTab = "style =
\"display:none\"";
else
oTabsItem.HideTab = "style =
\"display:block\"";
tabItemList.Add(oTabsItem);
}
return
tabItemList;
}
While rendering Tabs I added TabID
instead of hard coding(#tab1). Line
:03





  • >">
    <%# Eval("TabTitle") %>
    <%# Eval("OptionalText") %>




  • Than remove all the sublayouts things from
    page and add a placeholder on page. Line
    :01


    On parts Page I invoked
    Page_Init Event where i wrote following code
    protected void Page_Init(object sender, EventArgs
    e)
    {
    Database db =
    Sitecore.Context.Database;
    try
    {
    LF.SitecoreData.Tabs oTabs = new LF.SitecoreData.Tabs();
    List lstTabs = new
    List();
    string tabName = string.Empty;
    lstTabs =
    oTabs.GetPartsTabs(Sitecore.Context.Item);
    foreach (TabsItem tab in lstTabs)
    {
    tabName = "/sitecore/layout/Sublayouts/Littelfuse/Products/Tabs/" + tab.TabTitle;
    Item slItm =
    db.Items[tabName];
    Sublayout sublayout =
    new Sublayout();
    sublayout.Path = slItm["Path"];
    phTabs.Controls.Add(sublayout);//Placeholder on the page
    }
    }
    catch (HttpException
    exc)
    {
    string message = "Exception while loading
    tabs Dynamically";
    Sitecore.Diagnostics.Log.Error(message, exc, this);
    }
    }


    Note: See line no 22, 23,
    By using these lines of code we would Log our all Exceptions in
    Sitecore.Diagnostics.

    You can also implemet dynamic tabs things with the
    same method, just make sure that TabTitle returns same name as SubLayout
    name.

    Hope you will not get any error but incase of
    errors do let me know.

    Comments