Tips Centre Saturday, April 20, 2024

Home Home | About Us About Us | Contact Us Contact Us

vrPakistanis Technologies - Web Development, Logo Designing, Web Designing

Home > ASP.Net

Select Specific Value WithIn Drop Down List Or Radio Button List

I want to share one very useful tip with you. When we have to select specific value in a drop down list or radio button list then we normally use the following syntax:

For Drop Down List
DropDownListID.Items.FindByValue("VALUE_HERE").Selected = True

For Radio Button List
RadioButtonListID.Items.FindByValue("VALUE_HERE").Selected = True

If the provided value does not exist then it gives the error. So, we write the above code in "Try Catch" block like this:

Try
  DropDownListID.Items.FindByValue("VALUE_HERE").Selected = True
Catch ex
End Try

Try
  RadioButtonListID.Items.FindByValue("VALUE_HERE").Selected = True
Catch ex
End Try

In my opinion the alternate and the best way to select the specific value is using the Text property of the drop down list or radio button list. The syntax is given below:

For Drop Down List
DropDownListID.Text = "VALUE_HERE"

For Radio Button List
RadioButtonListID.Text = "VALUE_HERE"

In the above code there is no need of using "Try Catch" block.

Tips Centre 2008-2021. All rights reserved. Developed by vrPakistanis Technologies.