Hassaan Masood
Hassaan Masood

Reputation: 145

DropdownList selectedIndex not saving

I have a dropdownlist in my asp.net page

<asp:RadioButtonList ID="rdblstcookinglevel" runat="server" CellPadding="0" 
    CellSpacing="0" >
    <asp:ListItem>Novice</asp:ListItem>
    <asp:ListItem>Beginner</asp:ListItem>
    <asp:ListItem>Intermediate</asp:ListItem>
    <asp:ListItem>Expert</asp:ListItem>
    <asp:ListItem>Professional</asp:ListItem>
</asp:RadioButtonList>

On Page load, the selectedvalue is set based on the value that comes from database.

rdblstcookinglevel.SelectedValue = user.CookingLevel;

But when on the page user changes selection, the selectedindex does not change, so the old selected value is sent back to database.

User.CookingLevel = rdblstcookinglevel.SelectedValue;

Upvotes: 2

Views: 1036

Answers (3)

MarkMeikle
MarkMeikle

Reputation: 1

I am posting here hoping this is helpful for others and also for my future reference. I was having issues with a dropdownlist not holding its selecteditem that I set in code. After doing some digging, I found that the mistake was my own (although based on a limitation of dropdownlist I was not aware of). I was dynamically populating the dropdownlist with items I created. I was giving each item unique text, but not a unique value. Apparently this causes confusion. So if you experience this problem, check that each dropdownlist item has a unique value.

Upvotes: 0

Ibrahem Ahmed Shehata
Ibrahem Ahmed Shehata

Reputation: 206

Make Sure to Assign a value to the dropdownlist Only in When the page is loaded the first time

Try this

If(!IsPostBack)
   {
       rdblstcookinglevel.SelectedValue = user.CookingLevel;
   }

this will prevent the old value from being sent to the database instead of the new one

Hope I Helped

Upvotes: 0

freefaller
freefaller

Reputation: 19963

Make sure the setting of the SelectedValue is only done on the first load of the page - and not done on the post back, i.e...

if(!Page.IsPostBack)
{
  rdblstcookinglevel.SelectedValue = user.CookingLevel;
}

Upvotes: 1

Related Questions