Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I am selecting an item in a drop down list by making it equal to the value in a database, however, when the user goes to select a new item and then press the update button, the old value is still displayed.

How can I make it so that once the value is changed, the item is changed?

The code I have is as follows:

dropdownlist1.SelectedIndex = dropdownlist1.Items.IndexOf(dropdownlist1.Items.FindByValue(query.Status));


"query" is a LINQ query.
Posted

Well, what you are doing looks right - depending on when it's happening during the page lifecycle - ASP.NET Page Life Cycle[^].

You also have to make sure that there are items in the dropdownlist1 before setting SelectedIndex - if the control is bound, this has to happen after binding has completed and not on postbacks.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Scrappy 1871 22-Jan-12 15:10pm    
Hiya,

I am doing the code in the Page_Load, with the values being added in the markup.
Espen Harlinn 22-Jan-12 15:16pm    
Check that the page is not in a postback before setting SelectedIndex.

if(IsPostBack == false)
{
dropdownlist1.SelectedIndex = dropdownlist1.Items.IndexOf
(dropdownlist1.Items.FindByValue(query.Status));
}
check whether dropdown list properties is autopostback=true;
and using page_load coding use
if(!page.Ispostback==true)
{
//change dropdownlist selectedindex value..
}
 
Share this answer
 
plz confirm that you have made autopostback==true
 
Share this answer
 
in design view clcik on smart tag .and check autopost back .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900