AllCategories.ascx Code Help for Navigation
Posted: Fri Oct 30, 2009 7:06 am
I'm trying to edit the code for how the categories are pulled/displayed in the AllCategories.ascx posted by mazhar around the forums. I'm trying to get 2 different options.
Option 1:
Load & display top level categories and for the current category, load child categories. This works for root categories, but if the user goes into a sub-category, the subcategories aren't displayed. It seems like I need to do something: If current category has a parent category, load those subcategories. I'm also not on the newest version of AC7 I don't think. If that matters.
What I have:
For my other option, I've been trying to figure out how to bind the subcategories of a root category to a nested repeater. Here's the display code that I've been trying to bind:
Thanks for any help - I've been working on this for days with little luck.
Dappy
Option 1:
Load & display top level categories and for the current category, load child categories. This works for root categories, but if the user goes into a sub-category, the subcategories aren't displayed. It seems like I need to do something: If current category has a parent category, load those subcategories. I'm also not on the newest version of AC7 I don't think. If that matters.
What I have:
Code: Select all
List<CategoryData> _CategoriesList = new List<CategoryData>();
int _CurrentCategoryId;
protected void Page_Load(object sender, EventArgs e)
{
_CurrentCategoryId = PageHelper.GetCategoryId();
CategoryCollection rootCategories = CategoryDataSource.LoadForParent(0, true);
_CategoriesList.Clear();
foreach (Category category in rootCategories)
{
CategoryData categoryData = new CategoryData();
categoryData.Category = category;
if (category.CategoryId == _CurrentCategoryId)
{
categoryData.CssClass = "category-tab active-tab";
_CategoriesList.Add(categoryData);
LoadChildCategories(category.CategoryId, 1);
}
else
{
categoryData.CssClass = "category-tab";
_CategoriesList.Add(categoryData);
}
}
CategoryRepeater.DataSource = _CategoriesList;
CategoryRepeater.DataBind();
}
protected void LoadChildCategories(int categoryId, int level)
{
CategoryCollection childCategories = CategoryDataSource.LoadForParent(categoryId, true);
foreach (Category childCategory in childCategories)
{
CategoryData categoryData = new CategoryData();
categoryData.Category = childCategory;
if (childCategory.CategoryId == _CurrentCategoryId)
{
categoryData.CssClass = "sub-category-tab active-tab";
// LoadChildCategories(childCategory.CategoryId, 1);
_CategoriesList.Add(categoryData);
}
else
{
categoryData.CssClass = "sub-category-tab";
_CategoriesList.Add(categoryData);
}
//LoadChildCategories(childCategory.CategoryId, (level + 1));
}
}
public class CategoryData
{
private Category _category;
public Category Category
{
get { return _category; }
set { _category = value; }
}
private string _cssClass;
public string CssClass
{
get { return _cssClass; }
set { _cssClass = value; }
}
}
Code: Select all
<asp:Repeater ID="CategoryRepeater" runat="server">
<HeaderTemplate>
<ul class="left-nav">
</HeaderTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li class='<%#Eval("CssClass") %>'>
<asp:HyperLink ID="CategoryLink" runat="server" NavigateUrl='<%#Eval("Category.NavigateUrl") %>' ToolTip='<%#Eval("Category.Summary") %>'><%#Eval("Category.Name") %></asp:HyperLink>
<asp:Repeater ID="ChildCategoryRepeater" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li class='<%#Eval("CssClass") %>'>
<asp:HyperLink ID="ChildCategoryLink" runat="server" NavigateUrl='<%#Eval("Category.NavigateUrl") %>' ToolTip='<%#Eval("Category.Summary") %>'><%#Eval("Category.Name") %></asp:HyperLink></li>
</ItemTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
</asp:Repeater>
Dappy