Avatar of Psyberion
PsyberionFlag for United Kingdom of Great Britain and Northern Ireland asked on

Setting an ASP:DropDownList.SelectedIndex

I have a bound DropDownList in an ASP Webapplication for which I need to set its' SelectedIndex and/or SelectedValue property programmatically in the VB.NET code behind.

e.g.
myDDL.DataSource = myDataSource
myDDL.DataTextField = "items"
myDDL.DataValueField = "values"
DataBind()

'Where myVal is the value of an item in the DropDownList
'Selecting via SelectedIndex:
For i = 0 To myDDL.Items.Count - 1
    If myDDL.Items(i).Value = myVal Then
        myDDL.SelectedIndex = i
    End If
Next

'Selecting via SelectedValue:
myDDL.SelectedValue = myVal

Now these 2 methods do set the properties - SelectedIndex and SelectedValue, but since the control is already rendered the item for 'myVal' does not show in the control. Instead I get the first item in the controls list, as if no selection has been made.

If I move the DataBind() to after setting the SelectedIndex or SelectedValue it resets them so that nothing is selected, so again I get the first item in the DropDownList showing.

How can I select an item from the DropDownList programmatically so that it shows the item as being selected in the control?
.NET Programming

Avatar of undefined
Last Comment
Psyberion

8/22/2022 - Mon
pillbug22

Try:

myDDL.SelectedIndex = myDDL.Items.IndexOf(myDDL.Items.FindByValue(myVal))
pillbug22

Oops - didn't read all the way...

You already get it to select the item, it just needs to be bound again.

Is this DDL in a dataGrid?  If so, just put this code (or yours) inside of the _ItemDataBound event for the grid.
ASKER
Psyberion

If I bind it again after selecting the item it still appears to have nothing selected, I'm not sure but I think the bind clears the SelectedIndex property.

The DDL is not in a datagrid no, just directly on the page.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
pillbug22

I just found a web app where I needed the same thing (DDL on the page), and I filled the DDL with my items, then changed the SelectedIndex property.

Are you sure it's passing a valid index/value to select?  Have you done an alert box or reponse.write to see what value it's trying to select?
ASKER CERTIFIED SOLUTION
dante469

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Psyberion

Dante, I'm afraid your gives me the same results as I had before. It's selecting the item (i.e. the SelectedValue holds the correct value) but it still doesn't show the selected item in the control.

I can tell this because I actually have 3 DDLs, and a selection on one of them determines what items are listed in the next. All three DDLs are only showing their first item.

e.g.: if the first list is food types {vegetables, fruit, meat} and I manually select 'fruit' the second list might then contain {banana, orange, apple}, selecting apple might give me a choice in the third DDL of {red, green} and suppose I select 'green'. Now if I made these selections programmatically my first DDL shows 'vegetables', my second DDL shows 'banana' and my third DDL shows 'red'. So the control is providing the right SelectedValue property, but it's just not showing it.

Now, the DDL is rendered in HTML as a <select> form input with <option> items. If I was doing this in PHP or just ASP I would simply render the control myself and check for when I reached the option I want, then I would simply put SELECTED in that options' tag.

If there isn't a method or property in the DDL object then it just reinforces my belief that these new languages are just dumbed down for lazy programming by wrapping everything in bulky code, and subsequently removing most of the functionality (similar to what frontpage/dreamweaver/etc. did to webpage design)....ok, grumble over.

Time to up the stakes a bit.....hmmm 100 points will do for now ;)
ASKER
Psyberion

Btw Dante, I'll give you an extra 50 points whether you give the final solution or not, as your code snippet was rather cleaner than mine, so I'm using that now.

Ta.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Psyberion

Ok, reason why the DDLs were only showing their first item is because of the DataBind() statements after setting the DataSource for each DDL. Some code/pseudocode using setDDL from Dante:

DDL1.DataSource = get_ddl1_items_from_db()
DataBind()
setDDL(DDL1,my_chosen_ddl1_item)

DDL2.DataSource = get_ddl2_items_from_db(DDL1.SelectedValue) <---items in DDL2 depend on chosen item in DDL1
DataBind() <---This clears the selection on DDL1! I did not know that :(
setDDL(DDL2,my_chosen_ddl2_item)

Changing the DataBind() statements (which I now gather binds all controls on the page and not just the last one?) with DDL1.DataBind() and DDL2.DataBind() respectively, the controls now display the correctly selected items.

So, thanks Pillbug22 and Dante.

Unfortunately I didn't see anything for splitting the points, as I wanted to give Pillbug22 100points and Dante 150, but it turns out it just gave Dante the 100 points....my bad. Any idea how I can transfer some more points to you guys?