sd_dracula
sd_dracula

Reputation: 3896

Asp.net autopostback

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

Answers (3)

M. Ali Iftikhar
M. Ali Iftikhar

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

Habib
Habib

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

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20775

Check for Page.ISPostBack on Page_Load event.

if (!page.IsPostBack)
{

//Fill the Drop down

}

Upvotes: 3

Related Questions