Avoiding dublicates in this scenario

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

hi

asp.net 2.0

This code adds childmenuitems to a menuitem in a Menu controll. The problem
is that after the user has clicked on the menuitem, hence the shildmenuitems
are already added to the Menu control, If user again clicks on the menuitem,
then the childmenuitems get added again etc...

I want the code to works so that if childmenuitem already exists then don't
add it again

How can I avoid dublicates in this scenario?

protected void Menu1_Click(object sender, MenuEventArgs e)
{
int id = Convert.ToInt32(e.Item.Value);
List<Category> categories = Category.GetCategories(id);
if (categories.Count > 0)
{
foreach (Category cat in categories)
{
MenuItem item = new MenuItem();
item.Text = cat.Name;
item.Value = cat.Id.ToString();
e.Item.ChildItems.Add(item);
}
}

}
 
Jeff,

Add in a: if (!Page.IsPostBack) so that your code only fires the first page
load.


--
Sincerely,

S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
Back
Top