We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Setting an ASP:DropDownList.SelectedIndex

Psyberion
Psyberion asked
on
Medium Priority
1,744 Views
Last Modified: 2012-06-27
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?
Comment
Watch Question

Try:

myDDL.SelectedIndex = myDDL.Items.IndexOf(myDDL.Items.FindByValue(myVal))
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.

Author

Commented:
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.
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?
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
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 ;)

Author

Commented:
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.

Author

Commented:
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?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.