Avoiding dublicates in this scenario

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);
}
}

}
 
S

S. Justin Gengo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top