Reputation: 3896
Is there a way to add items to a dropdownlist (or any other control with lists) and when SelectedIndexChange happens, it doesn't keep adding all the items everytime that event happens? I suppose one way would be to clear the list before the add code but is there another?
Upvotes: 0
Views: 1343
Reputation: 3155
I presume you are binding drop down on PostBack, inside PageLoad event. Use
if(!Page.IsPostback)
{
//do the binding here ...
}
and when your page postbacks (after the drop down value has changed, it wont bind again, as the page is not a new load, its now a post back)
Upvotes: 2
Reputation: 223402
It looks like you are adding items to the list on Page_Load
event. If you are doing that, in that case when ever SelectedIndexChanged
occurs it will add the items again. You can check if its a PostBack don't add items.
if(!Page.IsPostBack) // First time only
{
//Add items to list
}
Upvotes: 3
Reputation: 20775
Check for Page.ISPostBack
on Page_Load event.
if (!page.IsPostBack)
{
//Fill the Drop down
}
Upvotes: 3