从代码隐藏动态填充 RadMenu
Populating RadMenu Dynamically from Code Behind
我正在尝试在我的后台代码中动态填充 Telerik Radmenu 控件。我真的很难过。我需要能够将我的类别绑定到我的根元素,并将属性绑定到子元素。我真的迷失了。如果有人建议有更好的方法来执行此操作,即使它使用不同类型的菜单,我也很乐意尝试一下。提前致谢。
**编辑我的代码已更改。
protected void createFilter(int categoryid)
{
// check cateogyrid
//get list of proudct id
List<int> productIds = new List<int>();
DataRow[] productRow = CategoriesProductsData.Tables["Products"].Select("Category_ID = " + 573);
productIds = productRow.Select(p => int.Parse(p["Product_ID"].ToString())).ToList();
//get attributes
ITCProductService pService = new TCProductServiceClient();
var productTuples = (pService.GetProductsAttributes(productIds));
List<Tuple<int, CustomAttribute>> customAttributes = new List<Tuple<int, CustomAttribute>>();
foreach (var productTuple in productTuples)
{
foreach (var attributeTuple in productTuple.m_Item2)
{
var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
customAttributes.Add(customAttribute);
}
}
List<CustomAttributeCategory> categories = new List<CustomAttributeCategory>();
var categoryTuples = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).Distinct();
foreach (var categoryTuple in categoryTuples)
{
var category = new CustomAttributeCategory(categoryTuple);
var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();
foreach (var attributeTuple in attributeByCategory)
{
var attribute = new CustomAttribute(attributeTuple.Attribute);
var attributeProductIds = customAttributes.Where(a => a.Item2.Attribute.AttributeId == attributeTuple.Attribute.AttributeId).Select(a => a.Item1).ToList();
attribute.ProductIds = attributeProductIds;
category.Attributes.Add(attribute);
}
categories.Add(category);
foreach (var cat in categories)
{
var itemCategory = new RadMenuItem(cat.Category.Name.ToString());
handsetMenu.Items.Add(itemCategory);
var itemAttribute = new RadMenuItem(cat.Attributes.ToString());
handsetMenu.Items.Add(itemAttribute);
}
}
}
<%--RAD MENU--%>
<telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true">
</telerik:RadMenu>
我认为 ItemDataBound 不是添加子项的正确位置。
是否要在每个根菜单项下添加相同的子项?
尝试在 DataBound 事件(在所有 itemDataBound 事件完成后触发)中迭代所有 RadMenu 项目。
我正在尝试在我的后台代码中动态填充 Telerik Radmenu 控件。我真的很难过。我需要能够将我的类别绑定到我的根元素,并将属性绑定到子元素。我真的迷失了。如果有人建议有更好的方法来执行此操作,即使它使用不同类型的菜单,我也很乐意尝试一下。提前致谢。
**编辑我的代码已更改。
protected void createFilter(int categoryid)
{
// check cateogyrid
//get list of proudct id
List<int> productIds = new List<int>();
DataRow[] productRow = CategoriesProductsData.Tables["Products"].Select("Category_ID = " + 573);
productIds = productRow.Select(p => int.Parse(p["Product_ID"].ToString())).ToList();
//get attributes
ITCProductService pService = new TCProductServiceClient();
var productTuples = (pService.GetProductsAttributes(productIds));
List<Tuple<int, CustomAttribute>> customAttributes = new List<Tuple<int, CustomAttribute>>();
foreach (var productTuple in productTuples)
{
foreach (var attributeTuple in productTuple.m_Item2)
{
var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
customAttributes.Add(customAttribute);
}
}
List<CustomAttributeCategory> categories = new List<CustomAttributeCategory>();
var categoryTuples = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).Distinct();
foreach (var categoryTuple in categoryTuples)
{
var category = new CustomAttributeCategory(categoryTuple);
var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();
foreach (var attributeTuple in attributeByCategory)
{
var attribute = new CustomAttribute(attributeTuple.Attribute);
var attributeProductIds = customAttributes.Where(a => a.Item2.Attribute.AttributeId == attributeTuple.Attribute.AttributeId).Select(a => a.Item1).ToList();
attribute.ProductIds = attributeProductIds;
category.Attributes.Add(attribute);
}
categories.Add(category);
foreach (var cat in categories)
{
var itemCategory = new RadMenuItem(cat.Category.Name.ToString());
handsetMenu.Items.Add(itemCategory);
var itemAttribute = new RadMenuItem(cat.Attributes.ToString());
handsetMenu.Items.Add(itemAttribute);
}
}
}
<%--RAD MENU--%>
<telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true">
</telerik:RadMenu>
我认为 ItemDataBound 不是添加子项的正确位置。
是否要在每个根菜单项下添加相同的子项?
尝试在 DataBound 事件(在所有 itemDataBound 事件完成后触发)中迭代所有 RadMenu 项目。