Click here to Skip to main content
16,017,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I have dropdownlist that it get its items from DB in codes.

now I want one of the items be selected when I open the page,everytime(for example in my list have,2000,204,209,2012 ,now I want 2012 was select when open this page.

what do i do?
thanks
Posted
Comments
[no name] 22-Jul-12 14:29pm    
Setting the SelectedIndex might be a good start.

1 solution

What about:
C#
myDropdown.SelectedValue = "2012";

Refer: MSDN: ListControl.SelectedValue Property[^]
 
Share this answer
 
v2
Comments
veusk 22-Jul-12 14:35pm    
thanks, but dropdown get its items from db, I want selected end item in dropdown
Sandeep Mewara 22-Jul-12 14:42pm    
1. You can set any item as selected irrespective of where from you retrieved.
2. It sounds like you are looking to set the last item as selected always. If so,
myDrodown.SelectedIndex = myDrodown.Items.Count-1;
BTW, do put a check if count>0
veusk 22-Jul-12 14:56pm    
sorry how can I chesh count>o?
Sandeep Mewara 22-Jul-12 15:52pm    
Dude you need a book!

count the items before subtracting 1 and setting it as it will throw an error if it is zero
veusk 22-Jul-12 14:56pm    
and what is it ddl?

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