tags:

views:

69

answers:

1

Hello! I am new in ASPNET. I am having a problem returning a default value for a dropdownlist. When a user select another value for example

(Names- Value)Private - 1; Friends - 2; Public - 3; (not binded to database but hard coded)

When a user select a value and saved it to a database. How can I return a new dafault value for a dropdown?

I tried

dropdownlist1.selectedvalue = 2 (code behind)

and still the dropdownlist view Private instead of Friends.

What will be the solution to this scenario? Please help

Kind Regards Jake

A: 

This is entirely from my memory, but I believe that ".SelectedValue" actually requires the value, not the index. So you could try dropdownlist1.selectedvalue="Friends - 2". Or you could try dropdownlist1.items.findbyvalue("Friends - 2") in combination with dropdownlist1.selectindex(). Hope some part of that helps!

Charles Y.
Hi Charles!Inside the Page_loadI tried to put your suggestion something like thisdpdAccountPrivacy.items.findvalue(curValPrivacyFlagTypes(oDpdvalues).Rows(0)("VisibilityLevelID") )dpdAccountPrivacy.selectindex() ' it doesn't work....
Sorry that I wasn't more specific Jake. I looked up the relevant pages on MSDN and here is what I think the solution is:`dpdAccountPrivacy.SelectedIndex = dpdAccountPrivacy.Items.FindValue(curValPrivacyFlagTypes(oDpdvalues).Rows(0)("VisibilityLevelID"))`You could also do it this way:`dpdAccountPrivacy.SelectedValue = curValPrivacyFlagTypes(oDpdvalues).Rows(0)("VisibilityLevelID")`
Charles Y.
I just read through the whole thread again, and realized that you indicated your `curValPrivacyFlagTypes(oDpdvalues).Rows(0)("VisibilityLevelID")` item returned an Integer. Herein lies your problem. The Dropdownlist can either assign a selected item based on value/text or index. Value in this case is the value of an object, so the value of a string "Friends" for instance would be the string "Friends" and the index would be 2. You need to do this: `dpdAccountPrivacy.SelectedIndex = curValPrivacyFlagTypes(oDpdvalues).Rows(0)("VisibilityLevelID")`
Charles Y.
Thanks Charles! Problem solved!!!
Glad I could help! Would you mind checking the accepted answer check? I need all the reputation I can get :-)
Charles Y.