Connecting Tech Pros Worldwide Help | Site Map

Setting selected Index

Member
 
Join Date: Nov 2006
Posts: 44
#1: 4 Weeks Ago
I know that you can set set values to a drop down list as the page loads but how do I set the value selected index of a drop down list after the page loads in ASP.NET?
Thanks for the anticipate response.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: 4 Weeks Ago

re: Setting selected Index


There are 2 "page loads", one that happens on the server, and one that happens when the page is loaded in the browser (client side).

Which page load are you talking about?
Do you want to do this when the page is loaded client-side?
Or do you want to do this when the page is loaded server-side (maybe after you populate the DropDownList)?

If you want to do it server side there's a couple of different ways you can do it.
You can use the DropDownList's Items property and set the selected index like so:

Expand|Select|Wrap|Line Numbers
  1. myDropDownList.Items(0).Selected = True
Or you could use the DropDownList's Items FindByValue method like so:
Expand|Select|Wrap|Line Numbers
  1. myDropDownList.Items.FindByValue("theValue").Selected = True
Or you could use the FindByText method....
Expand|Select|Wrap|Line Numbers
  1. myDropDownList.Items.FindByText("theText").Selected = True
-Frinny
Reply